← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google coding interview with a BST problem that looks clean on the surface but has a few edge cases that'll trip you up if you're not careful.

Questions Asked (1)

Q1

Given a BST and a key, find the largest key in the tree that is strictly smaller than the given key.

Algorithms & Data Structures
Author's notes

My first instinct was to do an inorder traversal and scan through, which works but they clearly wanted something that uses the BST structure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the BST property to traverse from the root, maintaining a candidate for the largest key smaller than the target. At each node, if the node's key is less than the target, update the candidate and move right; otherwise, move left. Continue until reaching a null node, then return the candidate.

Pro tip: Clarify edge cases upfront, such as when no such key exists or when the tree is empty, and discuss how to handle duplicates if the BST allows them. This shows thoroughness and prevents misunderstandings.

1. Clarify the problem

Confirm the definition of 'strictly smaller' and ask about edge cases like empty tree, no predecessor, or duplicate keys. Ensure you understand the expected return value if no such key exists.

2. Outline the BST property

Explain that in a BST, for any node, all keys in the left subtree are smaller and all keys in the right subtree are larger. This allows efficient search.

3. Describe the iterative approach

Start at the root with a candidate variable (e.g., null). While the current node is not null, compare its key with the target: if less, update candidate and move right; if greater or equal, move left.

4. Analyze complexity

State that the time complexity is O(h) where h is the height of the tree, and space complexity is O(1) for the iterative version. Mention that in a balanced BST, this is O(log n).

5. Handle edge cases and conclude

Discuss what to return if no such key exists (e.g., null or -1). Optionally, mention a recursive alternative and compare trade-offs.

Key Points to Mention

  • BST property: left subtree keys < node key < right subtree keys
  • Iterative traversal to achieve O(1) space
  • Maintaining a candidate for the largest smaller key
  • Time complexity O(h) and space complexity O(1)
  • Edge cases: empty tree, no predecessor, duplicates
  • Comparison with alternative approaches like inorder traversal

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