Find degrees of connection in a LinkedIn graph
Problem Description
You are given an undirected graph representing professional connections. The graph consists of n people, labeled 0 to n-1, and a list of undirected edges called connections, where an edge (a, b) signifies that person a and person b are directly connected (a 1st-degree connection).
Given two specific people, src (source) and dst (destination), your task is to find and return the minimum number of connections (degrees) required to reach dst from src.
Return Values
- Return
0ifsrcis the same asdst. - Return
1ifsrcanddstare directly connected. - Return
2ifsrcanddstare connected via one intermediate person, and so on for higher degrees. - Return
-1ifdstis not reachable fromsrc.
Optional Requirement
If explicitly asked, also return one shortest path (the sequence of people) from src to dst.
Constraints
1 ≤ n ≤ 1e5(number of people)0 ≤ |connections| ≤ 2e5(number of direct connections)- The solution must be efficient.
AI interview prep
Core dilemma
Efficiently finding the shortest path in an unweighted, undirected graph, while correctly handling graph representation, edge cases, and optional path reconstruction under given constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find degrees of connection in a LinkedIn graph
Problem Description
You are given an undirected graph representing professional connections. The graph consists of n people, labeled 0 to n-1, and a list of undirected edges called connections, where an edge (a, b) signifies that person a and person b are directly connected (a 1st-degree connection).
Given two specific people, src (source) and dst (destination), your task is to find and return the minimum number of connections (degrees) required to reach dst from src.
Return Values
- Return
0ifsrcis the same asdst. - Return
1ifsrcanddstare directly connected. - Return
2ifsrcanddstare connected via one intermediate person, and so on for higher degrees. - Return
-1ifdstis not reachable fromsrc.
Optional Requirement
If explicitly asked, also return one shortest path (the sequence of people) from src to dst.
Constraints
1 ≤ n ≤ 1e5(number of people)0 ≤ |connections| ≤ 2e5(number of direct connections)- The solution must be efficient.