0
Challenge · 74The Interview Itself

Dry-running before you say done

Interviewused in ~90% of apps

Rereading your code finds almost nothing, because you read what you meant rather than what you wrote. Tracing one small example by hand, in a table, finds nearly everything. It costs three minutes and it is the difference between finding your own bug and having it found for you.

Core idea. Pick the smallest input that exercises the interesting case, draw a table with one column per variable, and fill in a row per iteration by reading the code literally. Do not skip ahead and do not trust what you intended.
Playground, this isn't a screenshot
hand-trace a function that looks correct
for (let i = 0; i + k < nums.length; i++) {
  best = Math.max(best, sumOf(nums.slice(i, i + k)));
}
nums = [1, 2, 9, 4], k = 2
not started
step 1 of 3

The code reads correctly. Three rows of a table by hand show that the last window is never tried, and no amount of rereading does.

↑ play a little first, then dig in