← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE interview with a BST deletion problem. Pretty classic tree question but there are enough edge cases to trip you up if you're not careful about the two-child case.

Questions Asked (5)

Q1

Given the root of a binary search tree and a key value, delete the node with that key and return the (possibly new) root while keeping the BST property intact.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The leaf case and one-child case felt fine, but I fumbled a bit explaining the two-child case live.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the recursive search for the node to delete, then handle the three deletion cases (no child, one child, two children) with the two-child case using the in-order successor (or predecessor). Emphasize maintaining the BST property and discuss time/space complexity, including recursion stack.

Pro tip: Mention that using the in-order successor (leftmost node in right subtree) is a common choice, but the predecessor works equally well; clarify that the choice affects tree shape and could impact future performance. Also, note that deletion in a BST is not guaranteed to keep the tree balanced, so in production you might use a self-balancing tree like a Red-Black or AVL tree.

1. Clarify and Define

Restate the problem and confirm assumptions: the tree is a valid BST, the key may or may not exist, and we need to return the new root. Ask if duplicates are allowed or if the tree can be modified in place.

2. Search for the Node

Explain that you will recursively search for the node with the given key, comparing the key with the current node's value to decide whether to go left or right.

3. Handle Deletion Cases

Describe the three cases: (1) node with no children: simply remove it; (2) node with one child: replace the node with its child; (3) node with two children: find the in-order successor (or predecessor), copy its value to the node, and recursively delete the successor.

4. Implement and Test

Write clean recursive code, ensuring base cases (null node) are handled. Walk through an example, including edge cases like deleting the root or a leaf.

5. Analyze Complexity and Trade-offs

State that time complexity is O(h) where h is the tree height (O(log n) for balanced, O(n) for skewed). Space complexity is O(h) due to recursion. Discuss iterative alternative to reduce space, and mention self-balancing trees for guaranteed performance.

Key Points to Mention

  • BST property: left subtree values < node value < right subtree values.
  • Three deletion cases: no child, one child, two children.
  • In-order successor (or predecessor) for two-child case.
  • Time complexity O(h) and space complexity O(h) for recursive solution.
  • Edge cases: deleting root, leaf, or non-existent key.
  • Trade-offs: recursive vs iterative, and balanced vs unbalanced trees.

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

Q2

How does the time complexity change between a balanced BST and a completely skewed one?

Algorithms & Data Structures
Author's notes

Straightforward follow-up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining what a balanced BST and a completely skewed BST are, then compare the time complexities for core operations (search, insert, delete) in each case. Explain how the height of the tree determines the complexity, and conclude with the practical implications for performance.

Pro tip: Mention that balanced BSTs guarantee O(log n) worst-case time, while skewed BSTs degrade to O(n), and note that self-balancing trees like AVL or Red-Black trees are used in practice to avoid this degradation.

1. Define the structures

Briefly describe a balanced BST (height ~ log n) and a completely skewed BST (height ~ n).

2. Identify core operations

List the main operations: search, insert, and delete, and note that their time complexity depends on tree height.

3. Compare time complexities

For balanced BST, operations are O(log n); for skewed BST, they become O(n). Explain why: height difference.

4. Discuss practical implications

Mention that skewed trees lead to worst-case linear time, which is inefficient for large data, hence self-balancing trees are preferred.

5. Conclude with trade-offs

Summarize that balancing maintains efficiency, while skewness can occur with naive insertions (e.g., sorted order) and should be avoided.

Key Points to Mention

  • Height of balanced BST is O(log n), skewed BST is O(n).
  • Search, insert, delete time complexity is O(h) where h is height.
  • Balanced BST: O(log n) worst-case; skewed BST: O(n) worst-case.
  • Skewed trees can result from inserting sorted data into a naive BST.
  • Self-balancing trees (AVL, Red-Black) maintain O(log n) by keeping height logarithmic.
  • Practical impact: skewed trees cause performance degradation, especially for large datasets.

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

Q3

Can you rewrite your recursive solution iteratively?

Algorithms & Data Structures
Author's notes

Did not love this follow-up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain the recursive solution's logic and identify the implicit call stack. Then, simulate that stack using an explicit data structure (e.g., stack or queue) and a loop, ensuring you handle state and order of operations correctly. Finally, discuss trade-offs like space complexity and readability.

Pro tip: Mention that while recursion is often cleaner, iterative solutions can avoid stack overflow and sometimes improve performance. Also, note that the choice depends on the problem and constraints.

1. Understand the recursive solution

Clearly state what the recursive function does, its base case, and how it combines results. Identify the state that changes with each call.

2. Identify the implicit stack

Recognize that recursion uses the call stack to store local variables and return addresses. Determine what information needs to be stored to simulate this.

3. Choose an explicit data structure

Select a stack (for depth-first) or queue (for breadth-first) to mimic the call stack. Sometimes a simple loop with variables suffices for tail recursion.

4. Convert to iterative logic

Replace recursive calls with push/pop operations. Ensure the order of processing matches the original recursion (e.g., for tree traversals, push right then left for preorder).

5. Test and analyze

Walk through an example to verify correctness. Compare time and space complexity with the recursive version, noting any improvements or trade-offs.

Key Points to Mention

  • Call stack mechanics and how recursion uses memory
  • Explicit stack or queue implementation details
  • Handling of base cases and termination conditions
  • Order of operations (e.g., preorder, inorder, postorder) and how to preserve it
  • Space complexity: recursion uses O(h) stack space, iterative may use O(n) in worst case
  • Tail recursion optimization and when it applies

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

Q4

How would you handle duplicate keys in a BST deletion scenario?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

They asked this almost as an aside.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the definition of duplicate keys in the BST (e.g., whether they are allowed and how they are stored). Then explain the deletion strategy: either delete all occurrences, delete one occurrence, or handle duplicates by augmenting nodes with counts. Discuss trade-offs and choose an approach based on requirements.

Pro tip: Demonstrate awareness that duplicate handling is a design decision: many BST implementations disallow duplicates, but if allowed, a count field per node is often the most efficient. Mention that Google values clean, scalable solutions and clear communication of assumptions.

1. Clarify the problem

Ask whether duplicates are allowed in the BST and how they are represented (e.g., multiple nodes with same key, or a count field). Confirm the expected behavior when deleting a key with duplicates.

2. Choose a representation

Decide between storing duplicates as separate nodes (e.g., always in the right subtree) or augmenting each node with a count. Discuss the implications for search, insert, and delete.

3. Outline deletion strategies

For separate nodes: either delete all occurrences by repeatedly deleting the key, or delete one occurrence and leave others. For count field: decrement the count and remove the node only when count reaches zero.

4. Analyze trade-offs

Compare time complexity (e.g., O(h) for single deletion vs O(kh) for k duplicates), space overhead, and code complexity. Consider edge cases like deleting the last occurrence or when the node has two children.

5. Recommend a solution

Based on typical requirements (e.g., efficiency, simplicity), recommend using a count field per node as it handles duplicates elegantly and keeps the tree balanced. If duplicates as separate nodes are required, specify the deletion order.

Key Points to Mention

  • Definition of duplicate keys in BST: typically keys equal to the node's key are placed in the right subtree (or left, but be consistent).
  • Augmenting nodes with a count field to store frequency, which avoids multiple nodes with the same key.
  • Deletion when count > 1: simply decrement count; when count == 1, perform standard BST deletion.
  • Standard BST deletion cases: node with no children, one child, or two children (use inorder successor/predecessor).
  • Time complexity: O(h) for a single deletion, O(kh) for deleting k duplicates if stored as separate nodes.
  • Trade-offs: count field uses extra space but improves efficiency; separate nodes may simplify some operations but can lead to unbalanced trees if not handled carefully.

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

Q5

How does deletion work differently in a self-balancing tree like an AVL or Red-Black tree compared to a plain BST?

Algorithms & Data StructuresSystem Design
Author's notes

I knew the high-level answer (rotations, rebalancing after deletion) but I was pretty vague on Red-Black specifics.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the standard BST deletion cases, then highlight that self-balancing trees must restore balance after deletion via rotations and recoloring. Compare AVL and Red-Black tree deletion in terms of rebalancing strategies and complexity, and discuss the trade-offs in performance.

Pro tip: Emphasize that while deletion in self-balancing trees is more complex, it guarantees O(log n) worst-case time, which is crucial for real-time systems. Mention that Red-Black trees often require fewer rotations than AVL trees during deletion, making them preferable for write-heavy workloads.

1. Review BST deletion basics

Briefly outline the three cases in a plain BST: deleting a leaf, a node with one child, and a node with two children (using inorder successor/predecessor).

2. Explain the need for rebalancing

After deletion, self-balancing trees may violate their balance invariants (height balance for AVL, color properties for Red-Black), so they must perform rotations and/or recoloring to restore balance.

3. Detail AVL deletion

Describe how AVL trees rebalance after deletion by checking balance factors along the path to the root and applying single or double rotations. Note that deletion may require O(log n) rotations.

4. Detail Red-Black deletion

Explain that Red-Black trees use color flips and rotations to maintain properties. Deletion often involves fixing double-black nodes through cases, with at most O(1) rotations (though O(log n) recoloring).

5. Compare and contrast

Summarize key differences: AVL trees are more strictly balanced, leading to faster lookups but potentially more rotations on deletion; Red-Black trees have looser balance, resulting in fewer rotations and better performance for insert/delete-heavy workloads.

Key Points to Mention

  • Plain BST deletion has O(h) time and does not guarantee balance, leading to O(n) worst-case.
  • Self-balancing trees maintain O(log n) height, so deletion is O(log n) even in worst case.
  • AVL deletion may require multiple rotations up to the root, while Red-Black deletion requires at most 3 rotations (but O(log n) recoloring).
  • Red-Black trees are preferred in many standard libraries (e.g., C++ STL, Java TreeMap) due to fewer rotations on updates.
  • AVL trees provide faster lookups due to stricter balance, but may have slower deletion due to more rotations.
  • Both AVL and Red-Black trees use rotations, but Red-Black also uses recoloring to maintain properties.

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