← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Google SWE interview with a tree problem that looked like a BST question but wasn't. The invariants were broken so you couldn't trust the structure, which I think was the whole point.

Questions Asked (1)

Q1

You're given a ternary tree where each node can have up to three children (left, middle, right), originally built with BST-like ordering rules. But the tree may be corrupted and some nodes violate those rules. Find the mode of all node values in the tree.

Algorithms & Data Structures
Author's notes

My first instinct was to try to exploit the BST property and do something clever with the ordering, which was a mistake because the whole premise is that the invariants are broken.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the BST-like ordering is irrelevant for finding the mode, so we can ignore corruption and simply traverse the entire tree to count frequencies. Then, use a hash map to tally each node's value during a full traversal, and finally scan the map to find the most frequent value(s).

Pro tip: Mention that the mode can be multi-valued and discuss tie-breaking or returning all modes; also note that the tree's corruption doesn't affect the algorithm, showing you can separate irrelevant constraints from the core problem.

1. Clarify requirements and constraints

Ask whether the mode should be a single value or all values with maximum frequency, and confirm that the tree may be corrupted but we still need to traverse all nodes. Also discuss input size to choose an efficient approach.

2. Choose traversal method

Select a tree traversal (e.g., DFS or BFS) that visits every node exactly once. Since order doesn't matter for counting, any traversal works; iterative DFS avoids recursion depth issues.

3. Count frequencies with a hash map

During traversal, use a hash map (dictionary) to map each node value to its frequency. Update the count for each visited node.

4. Find the mode(s) from the frequency map

Iterate through the hash map to find the maximum frequency and collect all values that achieve it. Return the mode(s) as required.

5. Analyze complexity and edge cases

State time complexity O(n) and space complexity O(n) for the hash map (plus traversal stack/queue). Discuss edge cases: empty tree, all unique values, multiple modes, and large trees.

Key Points to Mention

  • The BST-like ordering is irrelevant because corruption means we cannot rely on it; we must examine all nodes.
  • Use a hash map to count frequencies in O(1) average time per insertion/lookup.
  • Traversal can be DFS (recursive or iterative) or BFS; iterative avoids stack overflow for deep trees.
  • Time complexity is O(n) where n is the number of nodes; space complexity is O(n) for the hash map and O(h) for traversal stack (h = height).
  • Handle multiple modes: either return all or define a tie-breaking rule (e.g., smallest value).
  • Consider memory constraints: if values are bounded, an array might be more efficient than a hash map.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.