Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Keys and Rooms



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Problem: Accessing All Rooms

Accessing All Rooms

Problem Statement

There are M rooms numbered from 0 to M-1, and you begin inside room 0. Each room contains a set of keys that can unlock other rooms.

Specifically, each room i holds a list of keys represented as keysList[i], where each key is an integer between 0 and M-1. Using a key with value k allows you to open room k.

Initially, all rooms are locked except for room 0. You are free to move between any unlocked rooms you have access to.

Your task is to determine if it is possible to enter every room starting from room 0.

Examples

Example 1:
Input: [[2], [3], [1], []]
Output: true
Explanation:
Start in room 0 and find key 2.
Go to room 2 and find key 1.
Go to room 1 and find key 3.
Go to room 3.
Since all rooms are accessible, return true.
Example 2:
Input: [[1, 4], [4, 0], [3], [2], []]
Output: false
Explanation:
Rooms 0, 1, and 4 are accessible, but rooms 2 and 3 cannot be reached.
Return false.

Constraints

  • 1 ≤ M ≤ 1200
  • 0 ≤ keysList[i].length ≤ 1200
  • The total number of keys across all rooms does not exceed 3500.