The Keys and Rooms interview question is a graph reachability problem. There are rooms, and you start in room 0, which is unlocked. Each room has a list of keys to other rooms. Your goal is to determine if you can eventually visit all the rooms.
Companies like Microsoft, Meta, and Nvidia use the Keys and Rooms coding problem to test a candidate's knowledge of basic Graph Traversal. It tests if you can correctly identify a directed graph structure and implement BFS or DFS to find all reachable nodes. It’s a core Graph interview pattern.
This problem is a direct application of Depth-First Search (DFS) or Breadth-First Search (BFS).
visited of size , and mark visited[0] = true.visited array are true.rooms = [[1], [2], [3], []]
rooms = [[1, 3], [3, 0, 1], [2], [0]], room 2 is isolated (only has a key to itself, but no one has a key to it). Result: False.visited set, which leads to cycles (Room 0 unlocks 1, Room 1 unlocks 0).contains() instead of a boolean array or set for the visited check.Reachability in a graph is always solved by BFS or DFS. Practice implementing these iteratively, as they are more robust than recursive solutions for graphs with thousands of nodes. This is a fundamental Graph interview pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Reorder Routes to Make All Paths Lead to the City Zero | Medium | Solve | |
| Flower Planting With No Adjacent | Medium | Solve | |
| Remove Methods From Project | Medium | Solve | |
| Unit Conversion I | Medium | Solve | |
| Graph Valid Tree | Medium | Solve |