It looks like the grid BFS everyone has written a hundred times, and the usual visited set is exactly what makes it wrong. The fix is one line and it is the whole point of the question.
Shortest path across a grid to collect every key, where locked doors open only once you hold the matching key.
It looks like the grid BFS everyone has written a hundred times, and the usual visited set is exactly what makes it wrong. The fix is one line and it is the whole point of the question.
Updated Aug 2026 · Grounded in real Forward Deployed Engineer interview loops and written to a senior-engineer editorial bar.
The reason this gets asked instead of plain grid BFS is that it quietly tests whether a candidate understands what a graph node is. Most people carry an intuition that a node is a position, so they mark cells visited and the search dies the first time it needs to walk back through a corridor holding a new key. Candidates who say 'the state is where I am plus what I hold' before writing anything are done in ten minutes; candidates who write the standard template first usually cannot see why it fails, because it fails silently by returning -1 rather than by crashing.
No comments yet — be the first to share your approach.
