← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Meta SWE onsite coding round, one problem with a follow-up that caught me a little off guard. The core question was straightforward BST stuff but the paired design extension pushed things into different territory.

Questions Asked (2)

Q1

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

Algorithms & Data Structures
Author's notes

Went recursive, which was the right call under time pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a recursive DFS that leverages the BST property to prune branches: if the current node's value is less than low, only recurse right; if greater than high, only recurse left; otherwise include the value and recurse both sides. This yields O(n) worst-case but often much faster in practice.

Pro tip: Mention that the pruning makes the algorithm O(k) where k is the number of nodes visited, which can be much smaller than n. Also, clarify that the sum can be computed without modifying the tree, and discuss handling large sums with appropriate data types.

1. Clarify the problem and constraints

Ask about edge cases: empty tree, low > high, negative values, and whether the tree is guaranteed to be a BST. Confirm the expected return type (e.g., integer, long).

2. Explain the BST property and pruning strategy

Describe how the BST property allows skipping entire subtrees: if node.val < low, all left descendants are too small; if node.val > high, all right descendants are too large.

3. Outline the recursive algorithm

Write pseudocode: if node is null return 0; if node.val < low return rangeSum(node.right); if node.val > high return rangeSum(node.left); else return node.val + rangeSum(node.left) + rangeSum(node.right).

4. Analyze time and space complexity

State that time is O(n) worst-case but O(k) where k is the number of nodes in range, and space is O(h) for recursion stack, where h is tree height.

5. Discuss iterative alternative and edge cases

Mention that an iterative stack-based approach can avoid recursion depth issues, and walk through an example to verify correctness.

Key Points to Mention

  • BST property: left subtree values < node < right subtree values
  • Pruning: skip subtrees that cannot contain values in range
  • Recursive DFS with conditional branching
  • Time complexity: O(n) worst-case, but O(k) with pruning
  • Space complexity: O(h) for recursion stack
  • Handling edge cases: empty tree, low > high, negative values

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

Q2

Follow-up: design a streaming interface that returns the product of the last K values in a sequence, handling the case where a zero appears in the window.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one tripped me up more than I'd like to admit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Design a streaming interface that maintains a sliding window of the last K values and efficiently computes their product. Handle zeros by tracking the count of zeros in the window and the product of non-zero elements, so the product is zero if any zero is present, otherwise the product of non-zeros. Use a queue to manage the window and update the product and zero count on each insertion and removal.

Pro tip: Discuss the trade-off between maintaining a running product and recomputing from scratch; the zero-handling approach avoids division by zero and keeps operations O(1) amortized. Also, clarify assumptions about data types (e.g., integers vs floats) and potential overflow.

1. Clarify requirements and constraints

Ask about the data type, whether K is fixed or dynamic, and if the product can overflow. Confirm that the interface should support adding new values and querying the product of the last K values.

2. Design the data structure

Use a queue (or circular buffer) to store the last K values. Maintain a running product of non-zero values and a count of zeros in the current window.

3. Define update logic for adding a value

When a new value arrives, add it to the queue. If it's zero, increment zero count; otherwise, multiply it into the running product. If the queue size exceeds K, remove the oldest value: if it's zero, decrement zero count; otherwise, divide the running product by it (or recompute if division is problematic).

4. Define query logic for product

If zero count > 0, return 0; otherwise, return the running product. Ensure that the product is only valid when the window has exactly K elements (or handle initial partial windows as per requirements).

5. Analyze complexity and edge cases

State that each operation is O(1) amortized. Discuss edge cases: K=0, K=1, all zeros, no zeros, and overflow handling (e.g., using modular arithmetic or big integers).

Key Points to Mention

  • Use a queue to maintain the sliding window of last K values.
  • Track zero count separately to avoid division by zero and correctly handle zeros.
  • Maintain product of non-zero elements for O(1) query when no zeros present.
  • Update product and zero count on both insertion and removal of elements.
  • Discuss time and space complexity: O(1) per operation, O(K) space.
  • Address potential overflow and data type considerations.

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