Open Every Locked Room Using Keys Found Inside Them
Last updated: November 29, 2026
Quick Overview
Rooms are labeled 0 to n-1 and only room 0 starts unlocked. Each room holds a list of keys to other rooms; return True if you can eventually enter every room.
0
0
0 solved
Rooms are labeled 0 to n-1 and only room 0 starts unlocked. Each room holds a list of keys to other rooms; return True if you can eventually enter every room.
A Google phone screen style reachability problem, which also mirrors a crawler that can only reach pages linked from pages it has already fetched. The interviewer wants to see you treat keys as directed edges and run a traversal from room 0.
What the Interviewer Expects
- See that keys are directed edges
- Traverse from room 0 with a stack or queue
- Count visited rooms and compare to n
- Ignore duplicate keys and keys to visited rooms
- Explain why order of opening does not matter
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the rooms that stay locked.
- What is the fewest rooms you must visit before a given room opens?
- What if some keys are single-use and opening a room consumes the key?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
rooms, whererooms[i]lists key labels found in roomi. - Start holding access to room 0 only.
- Return whether every room is reachable....
High-Level Design
Keys never expire, so once a room is reachable it stays reachable, and the set of rooms we can enter is exactly the set of nodes reachable from 0 in the directed graph i -> key. Order does not matte...