← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Meta software engineer interview with a mix of tree problems, dynamic programming, and string manipulation. Nothing too wild but the variety kept me on my toes.

Questions Asked (6)

Q1

Given a binary tree where each node contains a single digit, compute the total sum of all root-to-leaf numbers (where each path forms a multi-digit number).

Algorithms & Data Structures
Author's notes

DFS felt like the right move and it was.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a depth-first search (DFS) traversal, passing the current accumulated number down the recursion. At each leaf, add the accumulated number to a running total. This approach is intuitive and efficient, with O(n) time and O(h) space.

Pro tip: Clarify edge cases upfront, such as an empty tree or a tree with a single node, and mention that you can optimize space by using an iterative approach with a stack if recursion depth is a concern.

1. Understand the problem

Confirm that each root-to-leaf path forms a number by concatenating digits, and the goal is to sum all such numbers. Ask clarifying questions about input constraints and edge cases.

2. Choose traversal method

Select DFS (preorder) to naturally build numbers along paths. Explain that BFS is also possible but requires storing partial numbers in the queue.

3. Define recursive function

Create a helper function that takes a node and the current number formed so far. Update the number as current * 10 + node.val, and if it's a leaf, return that number; otherwise, recurse on children and sum results.

4. Handle base cases

If the node is null, return 0. If the node is a leaf, return the current number. This ensures correct summation.

5. Analyze complexity

State that time complexity is O(n) since each node is visited once, and space complexity is O(h) for recursion stack, where h is tree height.

Key Points to Mention

  • Depth-first search (DFS) with preorder traversal
  • Accumulating the number as current * 10 + node.val
  • Identifying leaf nodes (no children) as endpoints
  • Handling edge cases: empty tree, single node
  • Time complexity O(n) and space complexity O(h)
  • Potential iterative solution using stack to avoid recursion depth issues

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

Q2

Sort an array and return all indices where the target value appears.

Algorithms & Data Structures
Author's notes

Easiest one of the bunch.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: if the array is unsorted, sorting it will reorder elements, so the original indices are lost. Therefore, the problem likely means to find all indices in the original array where the target appears, or to return the indices in the sorted array. Discuss both interpretations and choose one based on interviewer feedback. Then, propose an efficient solution: for original indices, a simple linear scan suffices; for sorted indices, sort the array and use binary search to find the range of target values, then return all indices in that range.

Pro tip: Always clarify ambiguities before coding; interviewers value candidates who identify edge cases and ask questions. Also, consider mentioning that if the array is already sorted, binary search is optimal, but if not, sorting first may be unnecessary overhead.

1. Clarify the problem

Ask whether the array is sorted or unsorted, and whether the indices should refer to the original array or the sorted array. Confirm if duplicates exist and if the target may not be present.

2. Discuss approaches

For original indices: linear scan O(n). For sorted indices: sort O(n log n) then binary search O(log n) to find range, then collect indices. Compare trade-offs.

3. Outline algorithm

If original indices: iterate through array, collect indices where element equals target. If sorted indices: sort array (if needed), use binary search to find first and last occurrence of target, then return all indices in between.

4. Analyze complexity

State time and space complexity for each approach. For linear scan: O(n) time, O(1) extra space (excluding output). For sort+binary search: O(n log n) time, O(n) space if sorting a copy, or O(1) if in-place and output not counted.

5. Handle edge cases

Consider empty array, target not present, all elements equal to target, and large arrays. Discuss how to handle duplicates efficiently.

Key Points to Mention

  • Clarify whether indices refer to original or sorted array
  • Linear scan for original indices: O(n) time
  • Sorting first for sorted indices: O(n log n) time
  • Binary search to find first and last occurrence of target
  • Time and space complexity trade-offs
  • Edge cases: empty array, target absent, all duplicates

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

Q3

In a binary search tree, find the node value closest to a given target.

Algorithms & Data Structures
Author's notes

I fumbled the iterative version at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the BST property to traverse from the root, keeping track of the closest value seen so far. At each node, compare the node's value with the target, update the closest if needed, and move left or right based on whether the target is smaller or larger. This yields O(h) time and O(1) space.

Pro tip: Clarify whether the tree is balanced or not, and discuss how the algorithm adapts to skewed trees. Also, mention that you can stop early if you find an exact match, which can improve average performance.

1. Clarify the problem

Ask if the tree is a standard BST with unique values, and confirm that 'closest' means minimizing absolute difference. Also, check if the tree can be empty or if the target can be any integer.

2. Outline the approach

Explain that you'll traverse from the root, maintaining the closest value found so far. At each step, compare the current node's value with the target and update the closest if the absolute difference is smaller.

3. Detail the traversal logic

If the target is less than the current node's value, move to the left child; if greater, move to the right child. If equal, return immediately as it's the closest possible.

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 iterative traversal. Mention that in the worst case (skewed tree), h = n.

5. Handle edge cases

Discuss what to do if the tree is empty (return null or throw exception), and how to handle multiple nodes with the same closest difference (any is acceptable).

Key Points to Mention

  • BST property: left subtree values are smaller, right subtree values are larger.
  • Iterative traversal avoids recursion stack overhead.
  • Maintain a variable for the closest value and update it at each node.
  • Early termination if an exact match is found.
  • Time complexity O(h) and space complexity O(1).
  • Edge cases: empty tree, single node, target outside the range of values.

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

Q4

Given a string with parentheses and letters, remove the minimum number of parentheses to make it valid.

Algorithms & Data Structures
Author's notes

Stack-based approach: track indices of unmatched open parens, flag unmatched closes as you go, then remove whatever's left.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track indices of unmatched opening parentheses and a set to track indices of unmatched closing parentheses. After one pass, remove characters at those indices to produce a valid string. Alternatively, use a counter-based two-pass approach to identify the minimum removals without extra space.

Pro tip: Clarify whether you need to return any valid string or all possible valid strings; the latter requires backtracking and is more complex. Also, mention that the problem guarantees a unique answer if you remove the minimum number, but multiple valid strings may exist.

1. Understand the problem and constraints

Confirm that you need to remove the minimum number of parentheses to make the string valid, and that the string contains only parentheses and letters. Ask if you need to return the resulting string or just the count.

2. Choose an approach

Decide between stack-based (easy to implement, O(n) space) and counter-based (O(1) space) approaches. Explain the trade-offs.

3. Implement the algorithm

For stack: iterate, push indices of '(', pop for ')', mark unmatched. For counter: first pass left-to-right to remove excess ')', second pass right-to-left to remove excess '('.

4. Construct the result

Build the output string by skipping characters at indices marked for removal. Ensure the result is valid and has minimum removals.

5. Test and analyze

Test with edge cases: empty string, all parentheses, nested, multiple invalid. Analyze time and space complexity.

Key Points to Mention

  • Time complexity: O(n) for both approaches, space complexity: O(n) for stack, O(1) for counter (excluding output).
  • The stack approach naturally handles nested and sequential parentheses.
  • The counter approach requires two passes but uses constant extra space.
  • Edge cases: empty string, string with no parentheses, string with only letters, multiple invalid parentheses.
  • The problem guarantees that the minimum number of removals yields a unique valid string? Actually, it may not be unique, so clarify.
  • Letters are ignored and should remain in the output.

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

Q5

Design a data structure that answers range sum queries on an immutable integer array efficiently.

Algorithms & Data Structures
Author's notes

Prefix sums.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints: immutable array, multiple range sum queries, and efficiency requirements. Then propose a prefix sum array that precomputes cumulative sums, enabling O(1) query time with O(n) preprocessing and O(n) space. Explain the trade-offs and mention alternative approaches like segment trees or Fenwick trees, but emphasize that prefix sums are optimal for immutability.

Pro tip: Mention that for immutable arrays, prefix sums are the optimal solution because they achieve constant-time queries with linear preprocessing, and discuss how you would handle edge cases like empty ranges or large sums (e.g., using 64-bit integers).

1. Clarify requirements and constraints

Ask about the array size, number of queries, whether the array is truly immutable, and if updates are needed. Confirm that queries are frequent and that O(1) query time is desired.

2. Propose prefix sum array

Describe building a prefix sum array where prefix[i] = sum of elements from index 0 to i-1. Then a range sum from i to j is prefix[j+1] - prefix[i].

3. Analyze complexity and trade-offs

State that preprocessing takes O(n) time and space, and each query takes O(1) time. Compare with segment trees (O(n) build, O(log n) query) and explain why prefix sums are better for immutable arrays.

4. Discuss edge cases and implementation details

Cover empty ranges, large sums (use 64-bit integers), and how to handle negative numbers. Mention that the prefix array can be built in a single pass.

5. Conclude and offer extensions

Summarize the solution and optionally mention how to handle updates if the array were mutable (e.g., using a Fenwick tree).

Key Points to Mention

  • Prefix sum array construction: prefix[0] = 0, prefix[i] = prefix[i-1] + arr[i-1]
  • Query formula: sum(i, j) = prefix[j+1] - prefix[i]
  • Time complexity: O(n) preprocessing, O(1) per query
  • Space complexity: O(n) for the prefix array
  • Alternative data structures: segment tree, Fenwick tree (BIT) and their trade-offs
  • Handling large sums with 64-bit integers to avoid overflow

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

Q6

You can climb one or two stairs at a time, each with a cost. Find the minimum cost to reach the top of the staircase.

Algorithms & Data Structures
Author's notes

Classic DP.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a dynamic programming problem where the minimum cost to reach step i is the cost of step i plus the minimum of the costs to reach steps i-1 and i-2. Define the base cases carefully: you can start at step 0 or step 1 with their respective costs, and the top is beyond the last step. Then implement an iterative solution with O(n) time and O(1) space, explaining each step clearly.

Pro tip: Clarify the problem constraints upfront (e.g., whether the top is beyond the last step, and if costs are non-negative) and mention edge cases like empty or single-step staircases. This shows attention to detail and prevents incorrect assumptions.

1. Clarify the problem

Confirm the input format, what 'top' means (usually beyond the last step), and any constraints (e.g., cost values, staircase size). Ask if you can start at step 0 or step 1.

2. Define the state and recurrence

Let dp[i] be the minimum cost to reach step i. Then dp[i] = cost[i] + min(dp[i-1], dp[i-2]) for i >= 2, with base cases dp[0] = cost[0] and dp[1] = cost[1].

3. Optimize space

Since only the last two values are needed, use two variables to track the minimum costs for the previous two steps, reducing space complexity to O(1).

4. Implement and test

Write clean code (e.g., in Python) and walk through a small example like cost = [10, 15, 20] to verify the logic. Handle edge cases such as empty array or single step.

5. Analyze complexity

State that the time complexity is O(n) and space complexity is O(1). Mention that this is optimal for the problem.

Key Points to Mention

  • Dynamic programming approach with optimal substructure
  • Recurrence relation: dp[i] = cost[i] + min(dp[i-1], dp[i-2])
  • Base cases: dp[0] = cost[0], dp[1] = cost[1]
  • Space optimization using two variables instead of an array
  • Time complexity O(n) and space complexity O(1)
  • Edge cases: empty staircase, single step, and large input

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