← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Did a coding round at Meta for a software engineer role. One question, BST-based, felt manageable but I second-guessed myself more than I should have.

Questions Asked (1)

Q1

Given a binary search tree and a range [low, high], compute the sum of all node values that fall within that range.

Algorithms & Data Structures
Author's notes

My first instinct was to do a full traversal and just skip nodes outside the range, which works but ignores the BST property entirely.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the BST property to prune branches: if a node's value is less than low, only its right subtree can contain values in range; if greater than high, only its left subtree. Recursively sum values that fall within [low, high], visiting only necessary nodes. This yields O(n) worst-case but often much faster.

Pro tip: Mention that the algorithm runs in O(n) worst-case but typically O(k + h) where k is the number of nodes in range and h is the tree height, and note that it uses O(h) space for recursion. This shows you understand the practical performance beyond big-O.

1. Clarify the problem

Confirm that the tree is a BST, the range is inclusive, and node values are integers. Ask if the tree can be empty or if low > high.

2. Outline the recursive strategy

Explain that you'll traverse the tree, pruning subtrees that cannot contain values in range based on BST ordering.

3. Define the recursive function

Write a function that takes a node and returns the sum. If node is null, return 0. If node.val < low, recurse right; if node.val > high, recurse left; else include node.val and recurse both sides.

4. Analyze complexity

State that time complexity is O(n) worst-case but often better due to pruning, and space complexity is O(h) for recursion stack.

5. Test with examples

Walk through a small BST and range to verify correctness, including edge cases like no nodes in range or all nodes in range.

Key Points to Mention

  • BST property: left subtree values < node < right subtree values
  • Pruning: skip left subtree if node.val < low, skip right subtree if node.val > high
  • Inclusive range: sum values where low <= val <= high
  • Recursive DFS with pruning
  • Time complexity: O(n) worst-case, but often O(k + h) where k is nodes in range
  • Space complexity: O(h) for recursion stack

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