Reported October 2025
Googletree

Transform and Prune a Mode-Valued Binary Tree

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

The Google OA reported in October 2025 hides its trap in the order of operations. You compute the mode on the original values, prune the mode's subtrees, then reciprocal and swap whatever survives. It's a tree problem with a counting step bolted on, and the edge case that breaks a naive solution is doing the mode check after you've already turned values into 1/x. Mix those up and every example fails. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time. Read the spec once, slowly, and the code is short.

The problem

You are given a binary tree as a node table nodes. Row i is [value, leftIndex, rightIndex]; child index -1 means that child is absent, and row 0 is the root.
Find the mode of the original node values. If several values have the same maximum frequency, use the smallest such value. Remove every node whose original value equals the mode, together with its entire subtree.
For every retained node, replace a nonzero value x with 1 / x, leave a zero value unchanged, and swap its left and right child pointers. Mode comparisons always use the original values, before any reciprocal replacement.
Return the retained tree as a new node table in breadth-first order, with child indices reindexed for that returned table. Return an empty table if the input is empty or the root is removed.

Function
transformModeTree(nodes: double[][]) → double[][]

Examples
Example 1
nodes = [[4,1,2],[0,-1,-1],[2,3,4],[2,-1,-1],[3,-1,-1]]
return = [[0.25,-1,1],[0,-1,-1]]
Value 2 is the unique mode, so node 2 and its whole subtree are removed. The root becomes 0.25, and its original left child with value 0 moves to the right and remains 0.
Example 2
nodes = [[4,1,2],[2,-1,-1],[3,-1,-1]]
return = [[0.25,1,-1],[0.3333333333333333,-1,-1]]
All three values occur once, so the smallest tied value, 2, is the mode. After the root swaps its children, the node with value 3 is retained on the left while the node with value 2 is removed.
Example 3
nodes = [[2,1,2],[3,-1,-1],[2,-1,-1]]
return = []
Value 2 is the mode and appears at the root, so the root and the entire tree are removed.
Example 4
nodes = []
return = []
An empty input tree produces an empty output table.

Constraints
0 <= nodes.length <= 500
Every row has exactly three finite numbers [value, leftIndex, rightIndex].
Each value is between -10^6 and 10^6, inclusive, and has at most six digits after the decimal point. Signed zero is treated as 0.
Each child index is the integer -1 or an integer in [0, nodes.length - 1].
For a nonempty input, row 0 is the root; every other row is reachable exactly once, so the table represents one valid binary tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The plan has four passes. First, count the original values in a hash map and pick the highest frequency, with ties going to the smallest value. Compare values as numbers, and treat -0 as 0. Second, run a BFS from row 0. If the root equals the mode, return an empty table. Otherwise, skip any child whose original value equals the mode, which drops its whole subtree automatically. Third, for each kept node, store 1/x unless x is 0, and swap the children when you enqueue them, so the old right child gets visited first. Fourth, assign new indices in BFS order and write -1 for absent children. The classic pitfall is reindexing: you need a queue that tracks each kept node's new index before its children are placed. Also handle the empty input first. StealthCoder is the hedge if the index bookkeeping slips on the live OA.

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 Transform and Prune a Mode-Valued Binary Tree 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.

Transform and Prune a Mode-Valued Binary Tree FAQ

What's the trick in this Google tree problem?+

Separate the phases. Compute the mode from original values only, prune using those original values, then apply reciprocal and swap during a BFS rebuild. If you transform values first, the mode comparison breaks and your pruning removes the wrong nodes.

How hard is this really?+

Medium at most. Nothing exotic is involved. It's a frequency count, a BFS, and careful index assignment. With only 500 nodes, performance isn't a concern. The difficulty is the spec's many small rules, so most lost points come from misreading, not from algorithms.

How do I handle ties for the mode?+

Build a frequency map, track the maximum count, then take the smallest value among keys with that count. Example 2 shows it: all values appear once, so 2 wins. Normalize -0 to 0 before counting so they share a key.

How do I produce the output indices in BFS order?+

Use a queue of original node indices. When you pop a kept node, assign it the next output slot. After the swap, push its kept children and record their future positions, which are the current output size plus queue offsets. Fill -1 for removed or absent children.

How do I prepare for this in 48 hours?+

Write a small BFS tree rebuild from an index table, then practice a frequency count with tie-breaking. Test the empty input, root removal, and a zero value. Those cases cover the traps. Keep the three phases as separate functions so debugging stays easy.

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