Shortest Path to Get All Keys
A hard-tier problem at 54% community acceptance, tagged with Array, Bit Manipulation, Breadth-First Search. Reported in interviews at Roku and 2 others.
You're locked in a grid with keys scattered around. You need the shortest path to collect every single one before you can escape. Roku, Airbnb, and Pinterest have all asked this. The naive BFS approach fails because you can't just visit each key in any order, walls block you until you have the right key. The trick is treating the set of collected keys as part of your state, not just your position. That's where bit manipulation enters. This problem sits at the intersection of graph traversal and state compression, and it's easy to get stuck on the state representation if you haven't drilled it. If this problem hits your live assessment and you blank on how to encode the key collection state, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Shortest Path to Get All Keys"
Shortest Path to Get All Keys is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe core pattern is BFS, but your state can't be just (row, col). It has to be (row, col, keys_collected), where keys_collected is a bitmask. Each key gets a bit position. When you land on a key, you flip that bit. When you try to pass through a locked door, you check if the corresponding key bit is set. The trap most candidates fall into is treating this as a shortest path problem without realizing that revisiting the same (row, col) with different key sets can lead to different optimal solutions. You need a visited set that tracks (row, col, bitmask) tuples, not just positions. The search space explodes if you don't compress the state correctly. Standard BFS implementation works once you nail the state definition. StealthCoder handles the bitmask indexing and multi-state tracking so you don't have to debug it live.
Pattern tags
You know the problem.
Make sure you actually pass it.
Shortest Path to Get All Keys recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path to Get All Keys interview FAQ
How hard is this compared to standard BFS grid problems?+
Much harder. Standard BFS visits each cell once. Here you can visit the same cell multiple times with different key sets, so the state space is (rows × cols × 2^num_keys). The acceptance rate is around 54%, suggesting a real jump in complexity. The trick isn't the BFS, it's realizing you need to track key state as part of your position.
Why doesn't a greedy approach work, like visiting the nearest key first?+
Because walls change accessibility based on which keys you hold. The nearest key might be unreachable without first grabbing a key in the opposite direction. BFS explores all possible key collection orders simultaneously, guaranteeing the shortest path across all permutations. Greedy breaks this guarantee.
How do you handle the key state representation?+
Use a bitmask where bit i represents whether you've collected key i. Typically keys are labeled 'a' to 'f', so bit 0 = 'a', bit 1 = 'b', etc. When you step on a key, OR the bitmask with (1 << key_index). When you encounter a door, check if (bitmask & (1 << door_index)) is true before passing.
What about the visited set in multi-state BFS?+
Don't just track visited cells. Track visited (row, col, bitmask) tuples. The same cell is unvisited if you reach it with a different set of keys. This prevents revisiting identical states but allows meaningful re-exploration when your key set changes.
Is this still asked at Airbnb, Roku, and Pinterest?+
Yes, all three are in the confirmed list. It's a hard problem that shows up in senior and mid-level assessments. The 54% acceptance rate suggests it's still actively used and not considered 'too hard' to filter. Expect it in backend or full-stack roles.
Want the actual problem statement? View "Shortest Path to Get All Keys" on LeetCode →