Classifying 3 - In an Interview, Nobody Tells You the Name

The table in There Are 55 Coding Interview Problems reads left to right. The entry name comes first, and the Input and Output sit next to it.

An interview is the opposite. You get one problem statement, and no entry name comes with it. So for the list to be usable, it has to read right to left as well.

1. What You Can See in the Statement

Some things stand out as you read a statement. Not algorithm names — things that are simply written there.

If the statement saysCandidatesSplitting question
it is sorted · you may sort itTwo ends moving in · Boundary by monotonicityare you finding a pair or finding a boundary
a contiguous runWindow over a sequence · Largest runa run that satisfies a condition, or a run with the largest sum
the longest thing that only goes upLongest increasing subsequencemust it be contiguous
the first larger or smaller thing at each positionNext greater elementif an earlier element is smaller than the current one, is it useless forever
ranges asked about many timesPrefix and rangedo values change in between
the kthKth element · Pull the best one nextasked once, or asked over and over
the two or three that sum to the targetPair with targetmay you sort
how many times · a majority · a missing valueCount and tallyis the range of values narrow
you have to decide how to sort it firstSort then scandoes fixing one criterion make the rest simple
things with a start and an endIntervalssort by start, or sort by end
some tasks must come firstTopological ordera cycle means there is no answer
islands · blobs · are these two in the same blobConnected componentsdo the edges arrive all at once or one at a time
is there a cycleCycleis there one next, or several branches
cheapest way to connect everything · most you can pushSpanning and flowdoes it split into two sides
least cost · fewest stepsShortest pathare all weights 1, are any negative
a gridWalk a grid · Grid DP · Simulate on a griddoes it spread, go one way only, or repeat a rule
a table sorted along every row and every columnSearch a sorted gridis it still sorted if you flatten it into one row
rotate or flip the matrixTransform in placemust you use no extra space
a treeWalk a tree · Tree DPdoes one pass finish it, or do you choose for each subtree on the way up
traversal output is givenBuild a treewhere in the input is the root
a tree with smaller on the left, larger on the rightBinary search treedo you build the sorted sequence, or pull it out
the common ancestor of two nodesLowest common ancestorcan you use the sorted property
a palindromePalindromedo you expand from the middle or close in from both ends
brackets or operatorsBrackets and expressionsis there nesting
two strings or two arraysEdit distance · Common subsequencethe cost of fixing, or the thing they share
find a piece inside a long textPattern in textis there more than one piece
words starting with this prefixPrefix setare the queries repeated
are the letters just in a different orderSame lettersare you only checking whether two match, or grouping all of them
rewrite it by a ruleRewrite a stringdoes the rule finish in one pass, or apply again to what came out
you choose at every stepCarry state forwarddo you know the number of states in advance, or does it grow with the input
how many waysCount the ways · Combinatoricscount up one step at a time, or count it all at once with a formula
you merge intervals as you goInterval DPdoes it split on what you do last
you choose against a limit or a targetKnapsackdo you use the same item more than once
show me every arrangementList every arrangementcan you prune
the set of what you picked is the stateSmall n, every subsetis the subset enough as state, or must you carry where you are too
players take turnsWin or losedoes the board split into independent piles
everything is paired except oneBit trickshow many of each are paired up
primes · divisors · multiplesDivisors and primesdo you look at one number, or at a range
bases · digits · very large numbersDigits and basesdoes it go past the type range
draw at randomChance and samplingdo you know the total size in advance
a list of operations and a time limitDesign a structuredo you do the work on the way in, or on the way out
throw something away when it is fullCache and evictionis the eviction rule time or count
you push and pop at one end onlyStack and queue disciplinedo they also ask for something like the minimum
insert, delete and lookup must all be fastKey to valueis there also a random pick
hand them to me one at a timeIterate on demandmay you expand everything in advance
the nodes are chained togetherLinked list surgerydo you know the length in advance

The left column is visible before you know the answer. You can only reach Monotonic Stack after monotonic stacks have already come to mind. "The first larger thing at each position" is simply written in the statement.

One of part 2's 55 is missing here. Geometry has no signal you can recognize in a statement beyond "a shape shows up". It is worth knowing that some entries have a weak signal — for those, the only way is to know them on sight.

2. The Constraints Are Part of the Statement

When the size of n is written down, the complexity of the answer is nearly settled.

  n <= 20          2^n           look at every subset
  n <= 40          2^(n/2)       split in half and meet
  n <= 500         n^3
  n <= 5,000       n^2
  n <= 10^5        n log n       sorting . binary search . heap . segment tree
  n <= 10^7        n             one pass
  values to 10^9   log(value) or math    the number itself is large, not its size

But this narrows it down; it does not settle it. With n <= 20 it could be bitmask DP or it could be plain backtracking. It only cuts the candidates down to a few.

3. When There Is More Than One Candidate

This is where it really starts. When the signal has narrowed it to two or three and you do not know which to use, ask in this order.

  (1) what does it look like if you just try everything
  (2) why is that slow
  (3) what do you not have to redo
  (4) so what do you use
  (5) why is that correct

Try the shortest run whose sum is at least K.

  (1)  look at every run                        n^2
  (2)  every time the left end moves, you add the sum up from scratch
  (3)  the left end never has to go back -- extend right, then pull left
  (4)  sliding window
  (5)  once the sum reaches K you may pull the left in, and when it is too
       small you must extend the right. Neither side ever moves backwards,
       so you look at each element at most twice

(3) is the answer. The rest are questions for getting there.

And if (3) does not come, you do not know that problem yet. Whether the name came to mind has nothing to do with it. You can say "sliding window" and still be stuck the moment the problem changes a little, if you cannot do (3).

Mix in negative numbers and (3) breaks. Pulling the left in is no longer guaranteed to make the sum go down. That is why the splitting question for this entry is written the way it is.

4. How To Review

  cover the right, look only at the left   recall in this order: signal -> candidates -> splitting question
  get stuck? say (1) to (5) out loud       find out if you get stuck at (3) or at (4)
  look at the ones you got wrong more often
  come back later to the ones you got right   of course you get it right if you look again right away

When you mix, mix the ones you confuse with each other. Not a random shuffle. Window over a sequence and Largest run have almost the same Input and Output — put those two side by side. There is one question to ask: why does this one use that method when the one next to it does not?

Mixing Makes You Worse While You Practice

Someone measured this with math problems. They taught 18 college students how to find the volume of four unfamiliar solids. One group practiced grouped by type, the other with the four types mixed together. Same problems, same count, only the order was different.

  during practice         grouped  89%     mixed  60%
  test one week later     grouped  20%     mixed  63%

The reason they got them wrong matters. The grouped side did not make arithmetic errors — they picked the wrong formula. When they picked the right formula, they nearly always got the right answer.

Practice in a block and you never have to choose which kind of problem it is. You are told in advance. Do ten Largest run problems in a row and you start all ten knowing the answer. In an interview nobody tells you.

And yet the grouped side does better while practicing. 89% against 60%. So mixing feels worse. In another experiment, where people had to name the painter of a painting, 78% of the 120 participants did better when the paintings were mixed. And 78% said grouping was as good or better. They still said that after taking the test. What it feels like is the opposite of what works.

Both were measured on undergraduates in a lab. Rohrer & Taylor listed four limits of their own experiment. One of them matters most for interviews: the test problems had the same shape as the practice problems. So they did not measure whether mixing still helps on a problem you have never seen before.

And say it out loud. An interview is not a test of solving. It is a test of showing how you solve, and if you only ever practice quietly on your own, that part never gets trained. Saying (1) through (5) out loud reviews the problem and practices talking at the same time.

Sources

  • Rohrer & Taylor 2007 · Doug Rohrer, Kelli Taylor, "The shuffling of mathematics problems improves learning", Instructional Science 35:481-498. The figures above are from Experiment 2.
  • Kornell & Bjork 2008 · Nate Kornell, Robert A. Bjork, "Learning Concepts and Categories: Is Spacing the 'Enemy of Induction'?", Psychological Science 19(6):585-592. The figures above are from Experiment 1a.