Path Existence in an Undirected Graph
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hinges on one data structure: an adjacency list, or a union-find if you'd rather skip traversal. Bloomberg reported this one in February 2021, and it's the classic "is there a path between two nodes" check on an undirected graph with up to 200000 vertices and 200000 edges. If your OA invite lands soon, expect a graph question this plain. The trap isn't the idea, it's the edge cases: isolated vertices, cycles, repeated edges, and source equal to destination. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is very doable with a clean visited set.
The problem
You are given an undirected graph with n vertices numbered from 0 to n - 1. The array edges contains one unordered pair [u, v] for each edge between vertices u and v. Return true if a path connects source to destination. Otherwise, return false. A vertex always has a path to itself, including an isolated vertex. Function validPath(n: int, edges: int[][], source: int, destination: int) → boolean Examples Example 1 n = 3 edges = [[0,1],[1,2],[2,0]] source = 0 destination = 2 return = true The edge between 0 and 2 directly connects the two vertices. Example 2 n = 6 edges = [[0,1],[0,2],[3,5],[5,4],[4,3]] source = 0 destination = 5 return = false Vertices 0 and 5 belong to different connected components. Example 3 n = 1 edges = [] source = 0 destination = 0 return = true The source and destination are the same isolated vertex. Constraints 1 <= n <= 200000. 0 <= edges.length <= 200000. Every edge contains two valid vertex numbers. 0 <= source, destination < n. The graph may contain cycles, disconnected components, and repeated edges.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Two clean ways to solve it. First, build an adjacency list, then run BFS or DFS from source with a visited array and return true the moment you reach destination. Second, use union-find: union every edge, then compare find(source) and find(destination). Both run in near-linear time. Pitfalls are predictable. Recursive DFS can blow the stack at 200000 nodes, so go iterative or use BFS. Forgetting to add edges in both directions breaks the undirected assumption. Skipping the visited check loops forever on cycles and repeated edges. Return true immediately if source equals destination, which covers Example 3 with n = 1 and no edges. Union-find with path compression avoids recursion trouble entirely. If you freeze during the live OA, StealthCoder can hand you the working template, but the pattern is simple enough to write from memory.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Path Existence in an Undirected Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Path Existence in an Undirected Graph FAQ
How hard is Path Existence in an Undirected Graph really?+
It's easy. It's a basic connectivity check. The difficulty is only in handling scale and edge cases: up to 200000 vertices, cycles, duplicate edges, and isolated nodes. If you know BFS or union-find, you can finish in minutes.
What's the trick to solving it fast?+
Build an adjacency list with edges added in both directions, then BFS from source with a visited array. Return true when you pop destination. Handle source equal to destination up front. That's the entire solution.
Should I use DFS, BFS, or union-find?+
Any works in linear time. With n up to 200000, recursive DFS risks stack overflow in some languages. BFS with a queue or union-find with path compression is safer. Pick whichever you can write without bugs under pressure.
Is this graph pattern still asked since Bloomberg reported it in February 2021?+
Connectivity and traversal questions stay common in assessments because they test whether you can model input as a graph. Even if this exact problem doesn't show up, the same adjacency list plus visited set pattern carries over to many variants.
How do I prepare in 48 hours?+
Write BFS and union-find from scratch once each, then test on the three examples, including the isolated vertex case. Practice building adjacency lists from an edge array. That covers this problem and most of its close variants.