25/32
Part 7 of 7 · Follow-upsLearn and do

What interviewers ask next

Once the levels pass, expect the interviewer to change the rules of the grid. Here are the usual follow-ups, one at a time, starting with diagonals.

What about diagonals? Use eight offsets, and the distances shrink

The neighbors, o, of a cell, X
four offsets
.o.oXo.o.
eight offsets
ooooXoooo
eight, on two rocks
X##o

With eight offsets, X reaches o in one diagonal step, between two rocks that touch.

Use eight offsets instead of four. Distances shrink: corner to corner on a 10 by 10 grid is 18 steps with four neighbors and 9 with eight. Ask whether a diagonal step may squeeze between two walls that touch at a corner. With plain eight neighbors it can: on [".#", "#."], (0, 0) reaches (1, 1) in one step.

The code doesn't change. The facts that counted four sides do:

  • The worked example's never case becomes k above 8.
  • A tree's heat can go up eight times.
  • Marking a cell when it's popped now writes wrong counts, on top of the extra pops. A neighbor on the cell's own level can queue it again, with a count one too high.

In the editor, fill in EIGHT and write neighbors8(grid, r, c, squeeze). When squeeze is False, a diagonal step may not pass between two rocks that touch at a corner.