Expert C Programming

(Jeff_L) #1

then derives two inconsistent end-states using two different physical laws (i.e., conservation of charge
and conservation of energy in the case of the capacitors), and queries the hapless prospect on the cause
of the inconsistency.


The trick here is that at least one expression of the end-state uses a formula that involves integration
over the event separating the start and end conditions. In the real world this is fine, but in the
theoretical experiment posed it causes integration over a discontinuity (since the moderating effects
have been idealized away); hence, the formula is useless. Engineers are most unlikely to have
encountered this before. Yep, those massless springs and circuits without resistance will get you every
time!


Yet another curve ball was pitched in an interview for a position as a software consultant with a large
management consulting company. "What does the execve system call return, if successful?" was the
question. Recall that execve() replaces the calling process image with the named executable, then
starts executing it. There is no return from a successful execve hence there is no return value. Trick
questions are fun to pose to your friends, but they don't really belong in an interview.


Give Me a String at Random from This File


Another favorite Microsoft question. The interviewer asks the applicant to write some code that
selects one string at random from a file of strings. The classic way of doing this is to read the file,
counting the strings and keeping a record of the offset at which each begins. Then pick a random
number between 1 and the total number, go to the corresponding offset in the file, and that's your
string.


What makes it very hard to solve is that the interviewer sets the condition that you can only make one
sequential pass through the file, and you may not store any additional information like a table of
offsets. This is another of those questions where the interviewer is mostly interested in seeing how you
solve problems. He or she will feed you hints if you ask for them, so most people eventually get it.
How well you do depends on how quickly you get it.


The basic technique is to pick survivors, and recalculate as you go along. This is so com-putationally
inefficient that it's easy to overlook. You open the file and save the first string. At this point you have
one candidate string with a 100% probability of picking it. While remembering the first string, you
read the next string. You now have two candidates, each with a 50% probability. Pick one of them,
save it, and discard the other. Read the next string, and choose between that and the saved string,
weighting your choice 33% toward the new string and 67% toward the survivor (which represents the
winner of the previous two-way selection). Save the new survivor.


Keep on doing this throughout the entire file, at each step reading string N, and choosing between that
(with probability 1/N) and the previous survivor, with probability (N - 1)/N. When you reach the end
of the file, the current survivor is the string picked at random!


This is a tough problem, and you either have to figure out the answer with the minimum of hints, or
have cleverly prepared yourself by reading this book.


Some Light Relief—How to Measure a Building with a Barometer

We find these kinds of questions so much fun that we even pose them to ourselves in a non-computing
context. Sun has a "junk mail" e-mail alias for employees to share thoughts of random interest;
occasionally people will pose problems on this alias, and challenge other engineers to compete in
submitting the best answer. Here is one such puzzle, recently set.

Free download pdf