Skip to content
Decoder

Find if Path Exists in Graph

Easy

Problem

A graph has n nodes labelled 0 to n - 1 and a list of undirected edges, where [u, v] joins u and v. Return true if there is a path from source to destination, and false otherwise.

Example 1

Input
n = 3, edges = [[0, 1], [1, 2], [2, 0]], source = 0, destination = 2
Output
true

Example 2

Input
n = 6, edges = [[0, 1], [0, 2], [3, 5], [5, 4], [4, 3]], source = 0, destination = 5
Output
false

Constraints

  • 1 <= n <= 2 * 10^5
  • 0 <= edges.length <= 2 * 10^5
  • 0 <= source, destination < n
  • No duplicate edges and no self-loops

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.