Skip to content
RED SAGSkills

Is There a Corridor Route?

A building has n rooms numbered 0 to n - 1. Each entry [a, b] of corridors is a two-way corridor between room a and room b.

Return true if a person can walk from room start to room goal using the corridors, and false otherwise. A room can always reach itself.

Example 1

Input: n = 3, corridors = [[0,1],[1,2]], start = 0, goal = 2
Output: true
Explanation: 0 → 1 → 2.

Example 2

Input: n = 6, corridors = [[0,1],[0,2],[3,5],[5,4],[4,3]], start = 0, goal = 5
Output: false
Explanation: Rooms 3, 4 and 5 form a separate wing.

Constraints

  • 1 <= n <= 2 * 10^4
  • 0 <= corridors.length <= 2 * 10^4
  • 0 <= a, b, start, goal < n
  • No corridor connects a room to itself.
Hint 1

Build an adjacency list, then run a breadth-first or depth-first search from start.

Hint 2

Mark rooms as visited so each one is processed only once.

Good practice for: Backend Developer, Full Stack Developer, Game Developer, Site Reliability Engineer, Cloud Engineer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.