Questions I Want to Ask, but Can’t

The perfect interview coding problem is one that will be easy for a great candidate, hard for a good candidate, and impossible for everyone else. Unfortunately, most of the problems you come up with are going to be way too hard, and will have to be thrown away. It’s great if a candidate gets one right, but if she doesn’t then you don’t have any useful information.

That’s a shame, because these questions are fun, damn it! If only you could simplify them a little more… Well, I couldn’t figure out how to do that for these problems, so I present them to you, for your reading pleasure. Please feel free to send me code samples if you come up with good answers. :)

1. Given a binary search tree in which each node has a parent pointer, perform an inorder traversal without recursion, and without allocating any memory.

class BSTNode {
    BSTNode left, right, parent;
    int value;
class BST {
    BSTNode root;
    void inorder_traversal() {
        // what goes in here?

2. Write a function that takes an array of ints, randomly chooses one of them to be the pivot, and rearranges the array so that all of the ints less than the pivot are to its left, and all of the ints greater than or equal are to its right. The function should return the index of the pivot. (yes, this is the “partition” step of quicksort)

3. Same as #2, but do it with two pivots. (this is more fun :)

4. Classical Min/Max Heaps are typically represented using arrays, since they’re complete trees. Imagine, though, that you’re using a Min-Heap represented using nodes. If you know the number of nodes already in the Min-Heap, write an algorithm to describe the path to the next open spot. For example, given the following Min-Heap:

        /   \
      8      10
     / \    /  \
   12   9  13  20
  /  \
 18  15

You can get to the next open spot by going left-right-left from the root (i.e., to the left of the 9). If you know that there are nine elements in the tree, you should be able to come up with this path deterministically, without resorting to searching the tree, doing BFS, etc. Write that algorithm. (N.B. I agonized over different approaches to this problem, then one of my colleagues walked into my office and trivially solved it in under 30 seconds)


Updated: Don’t read the comments until after you’re tried the problems yourself!

4 thoughts on “Questions I Want to Ask, but Can’t

  1. For #4, if you convert the position you’re looking for into binary (i.e. position 9 becomes 1001) then reading from left to right after the initial 1 will give you the walk you need – 0 for left, 1 for right. So for example position 201 (binary 11001001) would be right, left, left, right, left, left, right.

  2. For #1: if we have a successor(node) function we just start with the leftmost node, get its successor, its successor, … until done. The successor function: if the node has a right sub-tree, grab the leftmost node in it; otherwise find the first ancestor that we’re in the left sub-tree of — if there is no such ancestor then we’re at the rightmost node so there is no successor.

    This should just be linear time on the whole.

Leave a Reply to Andrew Styles Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s