Reported July 2026
Googletree

Alternating-Color Binary Tree Roots

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's July 2026 OA looks like a rooted-tree puzzle, but it collapses to two cheap checks. Before you build any DFS, ask what "alternating by depth" means in a tree: every edge must join different colors. That's the whole bipartite condition. The other condition is a degree cap. This one was reported for Google in July 2026, and the n up to 200000 bound tells you a linear scan is the target. If you blank on the reduction during the live OA, StealthCoder runs invisibly as a safety net and surfaces it.

The problem

You are given a connected undirected tree with nodes numbered from 0 through n - 1. The array edges contains its undirected edges, and colors[i] is either B or W for node i.
A node is a valid root when rooting the tree there satisfies both conditions:
Every node has at most two children.
All nodes at the same depth have the same color, and adjacent depths alternate between B and W.
Return every valid root in increasing node order.
Interview follow-up
The interviewer also asked for the minimum number of node-color changes needed to make the conditions achievable. That follow-up is not part of this function's return value.

Function
findAlternatingBinaryRoots(n: int, edges: int[][], colors: String) → int[]

Examples
Example 1
n = 5
edges = [[0,1],[1,2],[1,3],[3,4]]
colors = "BWBWW"
return = []
Nodes 3 and 4 have the same color even though they are adjacent, so no root can make every adjacent level alternate.
Example 2
n = 5
edges = [[0,1],[1,2],[1,3],[3,4]]
colors = "BWBBW"
return = [0,2,3,4]
Every edge joins different colors. Node 1 has degree 3, so it would have three children if chosen as the root. Every other node has degree at most 2 and is a valid root.
Example 3
n = 4
edges = [[0,1],[0,2],[0,3]]
colors = "BWWW"
return = [1,2,3]
Any leaf can be the root: the center then has two children. The center itself is invalid because it would have three children.

Constraints
1 <= n <= 200000
edges.length == n - 1
Each edge contains two distinct node indices in [0, n - 1].
The edges form one connected acyclic graph.
colors.length == n, and every character is B or W.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: depth-alternating coloring holds for every root or for none. In a tree, depth parity of a node is fixed by the path from the root, so same-depth nodes share color only if all edges join different colors. Check every edge once. If any edge has equal colors, return an empty array. Otherwise, the child count rule decides. The root has degree children, while every other node has degree minus one children. So any node with degree 3 or more is a bad non-root, which means if any node has degree 4 or more, nobody works. Otherwise a node is valid as root only if its own degree is at most 2. Wait, check example 2: node 1 with degree 3 is just excluded as root, and others are fine. So the rule is: no node has degree above 3, and valid roots have degree at most 2. The pitfall is running DFS per root, which is O(n^2). The minimum color-change follow-up isn't returned, so skip it. StealthCoder is your hedge if the degree logic slips under pressure.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Alternating-Color Binary Tree Roots 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Alternating-Color Binary Tree Roots FAQ

How hard is the Google alternating-color roots problem really?+

Easier than it reads. It looks like a rerooting problem, but it reduces to an edge color check plus a degree check. If you see that, it's a single pass over the edges and the degree array. The difficulty is spotting the reduction, not the code.

What's the trick to solving it fast?+

Alternating by depth in a tree means every edge joins different colors, regardless of root. Check all edges once. Then use degrees: a root has degree children, every other node has degree minus one. That lets you decide all roots without any traversal.

Do I need DFS or BFS here?+

No. Trying each root with a traversal is O(n^2) and fails at n of 200000. You only need to count degrees and compare colors across each edge. Both are linear scans, so the whole solution runs in O(n).

What edge cases should I test?+

Test n of 1, which has no edges and the single node is a valid root. Test a star with a center of degree 3, where only leaves work. Test any node with degree 4 or more, which should return empty. Also test a path with a same-color edge.

How do I prepare for this in 48 hours?+

Practice recognizing when a rooted-tree condition is really a global property. Write the edge check and degree rule from memory, then run the three given examples by hand. Skip the minimum color-change follow-up, since the function doesn't return it.

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