Code RoomCollect all keys
HardPrep Room Coding #852

Collect all keys

CodingAlgorithms & data structuresSenior–Staff~40 min

Given a grid of characters where '.' is open, '#' is a wall, '@' is the single start, lowercase 'a'..'f' are keys, and uppercase 'A'..'F' are matching locked doors, return the fewest moves (up/down/left/right) to collect every key that exists in the grid. You may walk over a key cell to pick it up; you may only enter a door cell if you already hold its matching key. Return -1 if collecting all keys is impossible. There are at most 6 distinct keys.

Implement
shortest_path_all_keys(grid: list[str]) → int
Examples
in[["@.a.#","###.#","b.A.B"]]out8
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[["@.a.#","###.#","b.A.B"]]8not run yetsample