Shortest Path (Unweighted Graph) Problem
Shortest Path (Unweighted Graph) Problem — ExecCode Medium DSA Practice
Solve the Shortest Path (Unweighted Graph) problem on ExecCode. Free online medium DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given a directed unweighted graph, return the length of the shortest path from src to dst (number of edges), or -1 if unreachable. BFS explores in distance layers — the first visit to dst is optimal.
Examples
Input n = 7, edges = [[0, 1], [0, 2], [1, 6], [2, 3], [3, 4], [4, 5], [5, 6]], src = 0, dst = 6; Output 2. Input n = 3, edges = [[0, 1]], src = 0, dst = 2; Output -1. Input n = 4, edges = [[0, 1], [1, 2], [2, 3], [0, 3]], src = 0, dst = 3; Output 1
Constraints
1 ≤ n ≤ 10⁴ 0 ≤ edges.length ≤ 10⁵ 0 ≤ src, dst < n
Practice Shortest Path (Unweighted Graph) free on ExecCode. Browse DSA problems, topic map, and placement guides.