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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

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
DFS
directed graph
reachability
visited set
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: rooms, where rooms[i] lists key labels found in room i.
  • 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...


Submit Your Answer
Markdown supported