← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google coding interview, one question on BST traversal logic. Pretty standard but I fumbled the edge cases more than I'd like to admit.

Questions Asked (1)

Q1

Given a binary search tree, find the floor and ceiling values for a given target number.

Algorithms & Data Structures
Author's notes

I knew the general idea but kept second-guessing myself on the boundary conditions.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the definitions of floor and ceiling, then traverse the BST iteratively, updating candidate values based on comparisons with the target. Leverage the BST property to achieve O(h) time and O(1) space.

Pro tip: Explicitly state that floor is the greatest value ≤ target and ceiling is the smallest value ≥ target, and handle edge cases like when no floor or ceiling exists. This shows attention to detail and prevents misinterpretation.

1. Clarify definitions and edge cases

Confirm that floor is the largest value ≤ target and ceiling is the smallest value ≥ target. Discuss what to return if no such values exist (e.g., null, -1, or a sentinel).

2. Choose traversal method

Decide between iterative and recursive approaches. Iterative is often preferred for O(1) space, but recursive is simpler to code.

3. Initialize candidates

Set floor and ceiling to null (or appropriate sentinel). Start traversal from the root.

4. Traverse and update

While current node is not null: if node.val == target, set both floor and ceiling to target and break; if node.val < target, update floor to node.val and move right; if node.val > target, update ceiling to node.val and move left.

5. Return results

After traversal, return the floor and ceiling values. If either remains null, indicate that no such value exists.

Key Points to Mention

  • Time complexity O(h) where h is tree height, and space complexity O(1) for iterative approach.
  • Handling duplicates: if target equals a node value, both floor and ceiling are that value.
  • Edge cases: empty tree, target smaller than all nodes (no floor), target larger than all nodes (no ceiling).
  • Comparison with alternative approaches like inorder traversal (O(n) time) to highlight efficiency.
  • Potential follow-up: modify to find predecessor/successor, or handle multiple queries with preprocessing.
  • Code clarity: use clear variable names and comments, and test with examples.

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