Programming Interviews Exposed

by John Mongan and Noah Suojanen


Errata

Page 43, code sample (Thanks to Terence Kelley)
The ! sign should be removed after the if statement because otherwise it will follow a NULL pointer and not a non-NULL pointer, exactly what we want to avoid.
The corrected code:
void RemoveHead(node **head)
{
    node *temp;
    if (head && *head) { /* Corrected code */
        temp = (*head)->next;
        free(*head);
        *head = temp;
    }
}
Page 56, code sample (Thanks to Terence Kelley)
This code has a bug where the else if will always return 1 except in the case of a NULL list because fast is initially set to slow. This can be corrected by inserting code to guarantee that that clause is ignored on the first run through the loop.
The corrected code is:

   /* Takes a pointer to the head of a linked list and
    * determines if the list ends in a cycle or is NULL
    * terminated.
    */
   int DetermineTermination(node *head)
   {
       node *fast, *slow;
       fast = slow = head;
       int first = 1;
       while (1) {
           if (!fast || !fast->next)
               return 0;
           else if ((fast == slow || fast->next == slow) &&
!first)  /* New code */
               return 1;
           else {
               slow = slow->next;
               fast = fast->next->next;
               first = 0; /* New code */
           }
       }
   }
Page 165, second to last line
2 is not a perfect square and should not be included in the list of perfect squares between 1 and 100. With the exception of this, the explanation is correct.
Page 174, Figure 8.4
The labels are missing for the legend. The legend should be identical to that in Figure 8.3 on page 172.

If you have discovered an error in the book other than those listed above, please report it.