Expert C Programming

(Jeff_L) #1

You can't achieve this with a function. The type of a function's formal parameter is defined within the
function, so it cannot be passed via the call. Therefore you have to write a macro that will operate on
its argument according to the declaration of that argument.


The next point is to clarify whether the macro's argument is to be a type or a value of a type.
Assuming the argument is a value, the essential characteristic of an unsigned value is that it can never
be negative, and the essential characteristic of a signed value is that complementing its most
significant bit will change its sign (assuming 2's complement representation, which is pretty safe).
Since the other bits are irrelevant to this test, you can complement them all and get the same result.
Therefore, try the following:


#define ISUNSIGNED(a) (a >= 0 && ~a >= 0)


Alternatively, assuming the argument is to be a type, one answer would use type casts:


#define ISUNSIGNED(type) ((type)0 - 1 > 0)


The correct interpretation is key here! Listen carefully, and ask for a better explanation of any terms
that you don't understand or weren't well defined. The first code example only works with K&R C.
The new promotion rules mean it doesn't work under ANSI C. Exercise: explain why, and provide a
solution in ANSI C.


Most Microsoft questions have some element of determining how well you can think under pressure,
but they are not all overtly technical. A typical nontechnical question might be, "How many gas
stations are there in the U.S.?" or "How many barber shops are there in the U.S.?" They want to see if
you can invent and justify good estimates, or suggest a good way of finding more reliable answers.
One suggestion: call the state licensing authorities. This will give you exact answers with only 50
phone calls. Or call half-a-dozen representative states, and interpolate from there. You could even
respond as one environmentally-conscious candidate did, when asked "How many gas stations?" "Too
many!" was her annoyed response.


What Is the Time Complexity of Printing the Values in a Binary Tree?


This question was asked during an interview for a position on a compiler team with Intel. Now, the
first thing you learn in complexity theory is that the notation O(N) means that as N (usually the
number of things being processed) increases, the time taken increases at most linearly. Similarly, O(N^2 )
means that as N increases, the processing time increases much, much faster, by up to N-squared in fact.
The second thing that you learn in complexity theory is that everything in a binary tree has O(log(n)),
so most candidates naturally give this answer without even thinking about it. Wrong!


This turned out to be a question similar to Dan Rather's famous "What is the frequency, Kenneth?"
question—an enquiry designed to discomfit, confuse, and annoy the listener rather than solicit
information. To print all the nodes in a binary tree you have to visit them all! Therefore, the
complexity is O(n).


Colleagues have reported similar chicanery in an interview question for electronic engineers at
Hewlett-Packard. The question is posed in the form of a charged and uncharged capacitor suddenly
connected together, in an ideal circuit with no resistance. Mechanical engineers were asked the same
question about two massless springs displaced from equi-librium and then released. The interviewer

Free download pdf