Home
Updates
News

About Chris
Chris' Career
Pictures

The Best...
Thoughts On...
Feedback

Programming
    General
    C++
    Java
    Interview Questions
    Article
      by Orson Scott Card

Family & Friends
Favorite Links
FAQ

Archives

Programmer Interview Questions


Tal Moshaiov’s Question

Tal contributed this question after seeing the others I had posted here. He is a team leader from Israel that was (is?) looking for questions for Java programmer interviewees.

A man dies and arrives to the gates of the next world. He sees 2 gates. One of the gates leads to Heaven and one to Hell, however they’re unmarked. At each door stands a guard. We know that the guard to Hell always lies and that the guard to Heaven always tells the truth. The man is granted with one question to ask one of the guards to figure which gate should he enter. What is the proper question?

Solution

Back


Another Microsoft Question

You have one gold bar that you are going to use to pay a guy who works for you. You must pay him daily, so the gold bar has grooves to be split 6 times (making 7 pieces). Imagine it is kind of like a Kit Kat® bar.

You can only break the gold bar TWICE.

How do you break it twice and still manage to pay him for each day’s work?

Solution

Back


Westwood Studios (April 1999)

This test is what Westwood Studios faxes you if you request to interview for a programmer position. It is by far the most draconian programmer test I’ve come across. Most of the questions in it I could answer per their instructions. Some of them just make me think, when am I ever going to have to do this without referrence material and tools (like a calculator)?


This test was designed to determine speed, accuracy, and general problem solving abilities. When answering these questions, try to complete each problem swiftly and accurately. You may not use any reference materials nor a calculator, so please show all of your work.

  1. Write a ‘C’ function to insert a structure into a circularly linked list based on a key value (ascending order). You will be given a pointer to the current head of the list and a pointer to the new entry to insert. The structure is as follows:

    
    typedef struct tag {
    
    	int x;			// This is the key value
    
    	int y;
    
    	int color;
    
    	struct tag *prev, *next;
    
    } PointType;
    
    

    The prototype for the function is:

    PointType *InsertPoint(PointType *head, PointType *new_entry);

    The function should return a pointer to the head of the list. In this implementation, an empty list is defined as (head == NULL). Otherwise, the head node is the smallest key value, head->next is the node containing the next largest key value.

  2. Calculate the following arithmetic problems. Assume all numbers are base 16 and 16 bit quantities (show your work).

    1. C0 + 4ab
    2. FE * 2A
    3. FE / D
    4. 21-14

  3. Assume you want to get the modulus (remainder, or % in ‘C’), given x % y. You may also assume that y is always a power of 2. Show the fastest way to do this with a simple formula. Be specific about any arithmetic operations.

  4. Assume you want to test variable x (x is non-zero) to see if it is a power of 2. Show the fastest way to do this with a simple formula. Be specific about any arithmetic operations.

  5. How many iterations will the function Interesting_Test() be called given the following set of code?

    
    void How_Many_Times(void)
    
    {
    
    	int loop, value;
    
    
    
    	value = 0xABCD;
    
    
    
    	for (loop = 1; ((value >> 1) & 1) | (loop & 1); loop++)
    
    	{
    Interesting_Test(); if (loop & 1) { value >>= 1 ; } } }
  6. Write a function in assembly language (80x86, 6502, 65816, or 680x0) to compare memory against a given key value. The function will take a pointer to the beginning of a block of RAM that is WORD aligned (even), the size (8-bit quantity) of the memory block, and the key value (8-bit). The function must return TRUE (1) or FALSE (0) as to whether the block of RAM consists entirely of sequential repeating occurances of the key value. The function should be as fast as possible.

    The prototype for the function is:
    char VerifyRAMBlock(char *ram, char size, char key_value);

    You may assume register parameters or stack parameters (please specify which you choose and what registers or stack order the calling routine should use).

  7. Explain in English sentences an algorithm to solve the following problem:

    Assume there is a program that generates data sequences of 16 bytes each. These data sequences are to be stored in a large array in RAM until all processing is complete, and then written to out to disk. A requirement of the program is that all sequences written to disk must be unique. You may assume you have the following RAM available (given n data sequences):

    n * 16 bytes for data

    n * 4 bytes for long pointer data

    n * 2 extra bytes

    1K of general purpose RAM for variables

    You need to create a procedure that accepts and stores a single data sequence. Remember, it must not store duplicates, it must not use more than the given amount of RAM, and it must be as fast as possible. Explain (in English) how this routine would work, what data structure would contain the unique data sequences, and how to write them to disk.

  8. What is wrong with the following code? Please indicate where there are problems and what the corrections should be.

    
    
    1. /* This function takes an integer (16 bit) and a pointer to an allocated character array
    2. and converts the integer to a hex string. The string is of the form 0x1234 where 1234
    3. are hex digits. You may assume the character string is at least 7 bytes long. If i is
    4. zero, the string must be "0x0". Otherwise, leading zeros should be removed. */
    5. void itoh(int I, char *s)
    6. {
    7. int nibble, loop;
    8. *s++ = ‘0’;
    9. *s++ = ‘x’;
    10. /* Is the number 0? */
    11. if (i ==0)
    12. {
    13. *s++ = 0;
    14. }
    15. else
    16. {
    17. /* Go through the number nibble by nibble, starting with the most
    18. significant */
    19. for (loop = 3; loop > 0; loop--)
    20. {
    21. /* Get the nibble by shifting the number over by 12, 8, or 4 */
    22. nibble = (i>>(loop<<4))& 0x000F;
    23. /* Is the number in decimal range? */
    24. if (nibble <= 10)
    25. /* Convert the number to the character 0 – 9 */
    26. if (nibble || (*(s-2) != 'x'))
    27. *s++ = '0' + nibble;
    28. else
    29. ;
    30. else
    31. *s++ = 'A' + (nibble – 10);
    32. }
    33. }
    34. *s = '0'; /* null terminate it */
    35. }

Mike Reed’s Question

Assume you have eight (8) balls. The balls are identical in appearance (shape, size and color). All balls are also identical in weight except for one (1), which is slightly heavier than the other seven (7). If all you have to measure the balls is a balance, what is the fewest number of comparisons you can make to find the heavier ball?

Solution

Back


Josh Jensen’s Question

You need to reverse the order of bits in an 8-bit element (a byte). For example:

11001110

would convert to:

01110011

What is the absolute fastest way to find the reversed order any 8-bit byte?

Solution

Back


Gary Strawn’s Question

Gary doesn’t use this question anymore. It only applies to the old x86 processors. The new processors use multiple pipelines and this would not actually be the fastest method for such a processor. But I still like this question anyway. One just has to remember that it only applies to the older-type processors.

Suppose you need to swap the values stored in two registers. Assuming the processor does not have a swap command, what is the fastest way to transpose the values in the two registers?

Solution

Back


Mike Moore’s Question

You have two lengths of rope. Both ropes take exactly sixty (60) seconds to burn. But the speed at which they will burn varies along their lengths. The sections at which they vary are different for each length. The portions where the ropes burn fast and slow are not given. Despite the varying speed at which they burn, they both burn for exactly sixty (60) seconds before they are totally consumed.

Using nothing but the ropes, a lighter and a vessel of water (to extinguish the ropes) how can you determine when exactly fifteen (15) seconds has elapsed?

Solution

Back


The Classic Microsoft Question

Why are manholes round?

Solution

Back


Bonus Question

Here’s another question I got from Mike Reed or Mike Moore or perhaps even someone else—I don’t rightly remember who it was.

Suppose you have a round cake. It has a square portion cut from it. You must divide it into two equal portions. What is the proper way to do this with only one cut?

Solution

Back


Darren Eggett’s Question

You have 3 light switches in one room and 3 incandescent light bulbs in another room. What’s the fewest number of trips between rooms you can make to determine which switch controls which light?

Solution

Back


Email me your programmer interview questions!