Reported September 2026
ByteDancegraph

Path Existence in an Undirected Graph

Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ByteDance OA. Under 2s to a working solution.
Founder's read

The mistake that sinks most first attempts at this ByteDance OA question, reported in September 2026, is treating the graph like a tree and never marking visited nodes. Path Existence in an Undirected Graph looks like a warm-up, and it is. But cycles, repeated edges, and n up to 200000 punish sloppy code fast. You get an undirected graph, a source, a destination, and one boolean to return. If you blank on the traversal under pressure, StealthCoder runs invisibly on your screen during the live OA and hands you a working solution as a safety net. Know the pattern first, though. It's short.

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

The trick is connectivity, not path finding. You don't need the path, only whether source and destination share a component. Two clean options: build an adjacency list and run BFS or DFS with a visited set, or use union-find over the edges and compare roots. Pitfalls are concrete. First, forgetting visited marks makes cycles like [[0,1],[1,2],[2,0]] loop forever. Second, recursive DFS can blow the stack at 200000 nodes, so go iterative or use BFS. Third, handle source == destination up front, since an isolated vertex with no edges must return true. Fourth, repeated edges are harmless with a visited set but bloat the lists, so don't dedupe by hand. Both approaches run in roughly O(n + E). If the live OA freezes you, StealthCoder is the hedge that reads the problem and gives you the iterative version.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ByteDance's OA.

ByteDance 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 standard connectivity check. The difficulty comes from edge cases: cycles, disconnected components, repeated edges, and an isolated source equal to destination. If you handle those and avoid deep recursion at n = 200000, you're fine.

What's the trick to solving it fast?+

Build an adjacency list, then BFS from source with a visited array and return true when you reach destination. Or use union-find, union every edge, and compare roots. Either way you only need to know if they're connected, not the actual path.

Should I use DFS, BFS, or union-find?+

Any works in O(n + E). BFS is the safest since it avoids recursion depth issues. Union-find is shortest to write if you already have a template memorized. Pick the one you can type without thinking and don't switch mid-problem.

What edge cases break most solutions?+

Source equals destination with no edges, which must return true. Cycles without visited tracking. Recursive DFS overflowing the stack on a long chain of 200000 nodes. Also check you add edges in both directions, since the graph is undirected.

How do I prepare for this in 48 hours?+

Write BFS connectivity and union-find from scratch once each, then test on the three examples, especially the disconnected one and the single-vertex case. Keep the code iterative. That covers this question and most basic graph reachability variants ByteDance might reuse.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ByteDance.

OA at ByteDance?
Invisible during screen share
Get it