As reported: SentinelOne Senior Software Engineer (India, remote), 2025, technical round 2 (1.5 hours): a graph problem — are two nodes connected — solved with both DFS and a disjoint-set union (DSU). Follow-up: find the path with the minimum number of nodes traversed. The same round then moved to a high-level design.
This is LeetCode 1971 — Find if Path Exists in Graph. Our write-up: /find-if-path-exists-in-graph.
There is an undirected graph with n nodes labelled 0 to n - 1. edges[i] = [u, v] is an edge between u and v. There is at most one edge between any pair of nodes and no self-loops. Return true if there is a path from source to destination, otherwise false.
Example 1
n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
Output: true
Example 2
n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
Output: false
Nodes {0,1,2} and {3,4,5} form two separate components.
Constraints: 1 <= n <= 2 * 10^5, 0 <= edges.length <= 2 * 10^5, 0 <= u, v, source, destination <= n - 1, u != v.
Code runner format: four JSON values, one per line: n, edges, source, destination. Implement validPath(n, edges, source, destination) returning a boolean.
Follow-up
- Return a path from
sourcetodestinationthat passes through the minimum number of nodes (or report that none exists).