Reported November 2024
Googlebreadth first search

Get Components in Forest

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

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

Google threw this at candidates in November 2024 and it's a graph problem dressed up as tree counting. You've got a forest (disconnected trees) and need to return the size of each connected component. It sounds simple until you realize you need to track which nodes belong to which tree, handle the traversal correctly, and avoid off-by-one errors on component sizes. If you blank on the approach during the OA, StealthCoder gives you the pattern instantly so you can code with confidence instead of guessing.

Pattern and pitfall

The trick is recognizing this as a connected-components problem, not a tree problem. You're given a list of edges and a number of nodes, and you need to find how many separate trees exist and their sizes. DFS or BFS will work, but you have to track visited nodes carefully and only initiate a new search when you hit an unvisited node (meaning a new component). The gotcha: candidates often forget that some nodes might be isolated (no edges), so they need to count as components of size 1. If your component-tracking logic fails on the OA, StealthCoder supplies the correct traversal pattern so you can paste and adapt it.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Get Components in Forest 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of connected components in an undirected graph. If you have time before the OA, drill that.

⏵ The honest play

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

Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Get Components in Forest FAQ

Is this a union-find problem or a DFS/BFS problem?+

Both work, but DFS is simpler to code quickly. Union-find is more elegant if you're comfortable with it. For an OA where you've got limited time, DFS with a visited set is faster to write and debug. Either approach gets you the component sizes.

What's the catch with isolated nodes?+

If a node has no edges, it's still a component of size 1. You need to loop through all nodes and initiate a DFS/BFS from any unvisited node. If you only traverse from edges, you'll miss isolated nodes entirely and fail test cases.

How do I avoid double-counting edges?+

Mark nodes as visited as soon as you start processing them, not after. If you wait until after you explore neighbors, you risk processing the same node twice in the same component. Set visited[node] = True before you recurse.

What's the time complexity I should hit?+

O(n + e) where n is nodes and e is edges. You touch each node and edge exactly once. If you're seeing worse than that, you've got a bug in your visited tracking or you're re-exploring components.

Should I use recursion or iterative DFS?+

Recursion is cleaner and faster to write in an OA. Stack overflow is not a risk on Google's test sizes. Iterative is safer if you're paranoid, but recursion is the move here.

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

OA at Google?
Invisible during screen share
Get it