Easy · Graphs

Has Path From Source to Destination

Given a graph as adjacency lists adj and two nodes src and dst, return true if dst can be reached from src by following edges (always true when src equals dst), otherwise false.

Examples

Example 1

{
  "adj": [
    [1, 2],
    [0, 3],
    [0, 4],
    [1],
    [2, 5],
    [4]
  ],
  "pos": [
    [176, 44],
    [262, 94],
    [262, 193],
    [176, 242],
    [90, 193],
    [90, 93]
  ],
  "src": 0,
  "dst": 5
}

Output: path exists

Example 2

{
  "adj": [
    [1, 2],
    [0, 2],
    [0, 1],
    [4, 5],
    [3, 5],
    [3, 4]
  ],
  "pos": [
    [176, 44],
    [262, 94],
    [262, 193],
    [176, 242],
    [90, 193],
    [90, 93]
  ],
  "src": 0,
  "dst": 5
}

Output: no path

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Graphs problems