Reach Every Room Using Collected Keys

Quick Overview

Reach Every Room Using Collected Keys. Only room 0 is initially unlocked. rooms[i] lists the distinct room keys available inside room i.

Reach Every Room Using Collected Keys

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Only room 0 is initially unlocked. rooms[i] lists the distinct room keys available inside room i. Once visited, you may keep all of its keys and use them to enter further rooms. Return whether every room can be visited. ```cpp class Solution { public: bool canVisitAllRooms(vector<vector<int>>& rooms); }; ``` ### Examples **Example 1:** ```text rooms = [[2],[0],[1]] Output: true Explanation: Visit0, then2, then1. ``` **Example 2:** ```text rooms = [[1],[],[0]] Output: false Explanation: No reachable room supplies a key to room2. ``` ### Constraints - There are 2 through 1000 rooms labeled 0 through n-1. - Each room contains 0 through 1000 distinct valid keys. - Total keys across all rooms lie in [1,3000]. ### Contract considerations Start only at room 0 and retain directed key reachability. ```hint Read the boundary condition Which rooms become available after opening one room? ``` ### Boundary reminders Start only at room 0 and retain directed key reachability.

Overview: Reach Every Room Using Collected Keys. Only room 0 is initially unlocked. rooms[i] lists the distinct room keys available inside room i.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Only room 0 is initially unlocked. rooms[i] lists the distinct room keys available inside room i. Once visited, you may keep all of its keys and use them to enter further rooms. Return whether every room can be visited.

class Solution {
public:
    bool canVisitAllRooms(vector<vector<int>>& rooms);
};

Examples

Example 1:

rooms = [[2],[0],[1]]
Output: true
Explanation: Visit0, then2, then1.

Example 2:

rooms = [[1],[],[0]]
Output: false
Explanation: No reachable room supplies a key to room2.

Constraints

  • There are 2 through 1000 rooms labeled 0 through n-1.
  • Each room contains 0 through 1000 distinct valid keys.
  • Total keys across all rooms lie in [1,3000].

Contract considerations

Start only at room 0 and retain directed key reachability.

Boundary reminders

Start only at room 0 and retain directed key reachability.