From: alien@acheron.amigans.gen.nz (Ross Smith)
Newsgroups: comp.lang.c
Subject: Re: Testing competence of potential hires
Date: 14 Dec 92 12:32:34 GMT+12
Distribution: world
Organization: Muppet Labs

In article <1g9pm5INNosg@almaak.usc.edu> ajayshah@almaak.usc.edu (Ajay Shah) writes:
>I thought up two questions.  What holes can you poke in them?
>
>------------------------------
>
>(trivial competence measurement)
>   Consider this code fragment:
>
>   void abc(char *cde)
>   {
>      for (; *cde; ++cde) *cde = toupper(*cde);
>      return cde;
>   }
>
>   a) Roughly what is he trying?  (convert a string to uppercase).
>   b) What errors will the (ANSI) compiler choke on?
>        - it's a void and he's returning cde
>      What errors will the compiler not find?
>        - he is not checking if cde == NULL

And he's using *really* dumb names :-)

>   c) what header files are needed for this to compile? (string.h,ctype.h).

Wrong, it only needs <ctype.h>.

>------------------------------
>
>There is a C program which contains the line
>
>#include "min.h"
>
>   You are told it allows you to use a "function" min(a, b) which returns
>   the smaller of a and b (both integers).
>
>   Write a test program which will tell you whether min is a true function,
>   or a #define.  You are not allowed to know anything about how your
>   program is linked; you are only allowed to write this program, and
>   see it's execution results.
>
>Answer 1:
>   {
>        int i, j, s;
>        i=2; j=4;
>        s = min((++i), j);
>        printf("%d\n", s);
>   }
>
>Answer 2: try compiling
>   {
>        char p,q,s;
>        s = min(p, q);
>   }
>
>A1. If it's written as #define min(a,b) a<b?a:b then ++i will get
>executed twice, giving 4.  Otherwise the min will be 3.

Only works if it's an unsafe macro. I don't know any way offhand of writing
the minimum function as a safe macro ... anyone?

>A2: if this program compiles it's a hash-def, else it's a true function.

Only works on ANSI compilers (and not even then, if the function is defined
old-style, which is still allowed).

>Here are the others I hoarded off the net:
>
>1. Input is an array of strings.  The function should reverse the
>order of the strings in the array.
>
>2.  Input is a pointer to an array of 10 integers.  Sort the array.
>
>He can do it anyway he likes, and there is a spread in the
>possibilities: buggy < hand- written bubblesort < hand-written
>quicksort < using qsort(3).  This helps us seperate the men from the
>boys.

If somebody used qsort() on an array that was known to have only 10
elements, I'd suspect them of "programming by voodoo" rather than using
their brains.

>3. How do you set up .c and .h files ? What goes into the .h and what
>in the .c ?
>
>Building on this:
>
>How do you set up two .h files which are mutally dependend, that is:
>file A.h defines a structure type A that contains a pointer to B, file
>B.h defines a structure type B that contains a pointer to A.

Why would you want to?

>4. What tool will help you decipher 'char (*(*x())[])()'?
>
>answer: either "my brain" or "cdecl".
>
>cdecl is an acceptable answer, and would demonstrate some knowledge of
>tools to actually help get the job done.
>
>5. After
>       int i = 2;
>       i = ++i;
>   what does i equal?
>
>Answer: Nothing prevents the compiler from interpreting this as:
>
>        i = i + 1;
>        ++i;
>
>i.e., there is no reason the compiler can't decide to find what value
>is to be assigned to the i on the LHS side of the equation and only
>then look at the right hand side to increment i by 1.
>
>       x = ++i;
>
>means that x is assigned the incremented value of i and i is
>incremented, it in no way requires an ordering of these two
>operations.
>
>Another way of saying this is that there are no sequence points during
>the evaluation of i = ++i and i is assigned to twice. This makes the
>behavior of this expression undefined.
>
>
>Authors:
>
>1 and 2 are from wags@cimage.com
>3 is from dekker@dutiag.tudelft.nl (Rene Dekker)
>4 is from mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
>     and bandy@netnews.jhuapl.edu (Mike Bandy)
>5 is jfw@ksr.com (John F. Woods)
>     the answer is by dkeisen@leland.Stanford.EDU (Dave Eisen)
>--
>Ajay Shah, (213)749-8133, ajayshah@rcf.usc.edu

--
...... Ross Smith (Wanganui, NZ) ...... alien@acheron.amigans.gen.nz ......
   Mine is the ship bound for Alpha Centauri
   We must be out of our minds
   For though we are shipmates bound for tomorrow
   Everyone here's flying blind                          (Crystal Gayle)
--