← Meta Interview Insights

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

SeniorPrefer not to say
Apr 2026

Summary

Six coding problems back to back for a Meta SWE round, heavy on trees and graphs with some greedy/DP mixed in. The questions weren't impossible but the breadth meant you had to context-switch fast, and a couple of the follow-ups (especially the flight cost one) required more thought than I expected.

Questions Asked (6)

Q1

Given the root of a binary search tree and two integers representing a range, return the sum of all node values that fall within that range. How would you use DFS with BST-specific pruning, and what are the time and space complexities?

Algorithms & Data Structures
Author's notes

Pretty standard BST problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that you would perform a DFS traversal, but leverage the BST property to prune branches that cannot contain values in the range. Specifically, if the current node's value is less than the low bound, skip the left subtree; if greater than the high bound, skip the right subtree. Otherwise, include the node's value and recurse on both children.

Pro tip: Emphasize that pruning reduces the time complexity to O(n) in the worst case but often much less, and that the space complexity is O(h) for the recursion stack, where h is the tree height. Mention that this is optimal because you must visit each node in the range at least once.

1. Clarify the problem and constraints

Restate the problem: given a BST and a range [low, high], return the sum of all node values within the range. Ask about edge cases: empty tree, low > high, or range not overlapping with any node.

2. Outline the DFS with pruning approach

Describe a recursive function that takes a node and returns the sum. At each node, compare its value with low and high to decide whether to explore left, right, both, or neither, using the BST property to prune.

3. Detail the pruning logic

If node.val < low, then all values in the left subtree are also < low, so skip left and recurse only on right. If node.val > high, skip right and recurse only on left. Otherwise, include node.val and recurse on both children.

4. Analyze time and space complexity

Time: O(n) worst-case (e.g., all nodes in range), but pruning can reduce visits. Space: O(h) for recursion stack, where h is tree height; O(n) worst-case for skewed tree, O(log n) for balanced tree.

5. Discuss iterative alternative and trade-offs

Mention that an iterative stack-based DFS can avoid recursion depth issues, but recursion is simpler. Also note that if the tree is balanced, the average time is O(log n + k) where k is number of nodes in range.

Key Points to Mention

  • BST property: left subtree values < node.val < right subtree values
  • Pruning condition: if node.val < low, skip left; if node.val > high, skip right
  • Base case: if node is null, return 0
  • Time complexity: O(n) worst-case, but often better due to pruning
  • Space complexity: O(h) for recursion stack, h = tree height
  • Edge cases: empty tree, range not overlapping, low > high

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

Q2

Given a binary tree (not necessarily a BST) and two nodes p and q that are guaranteed to exist, return their lowest common ancestor. Walk through both recursive and iterative approaches and compare their complexities.

Algorithms & Data Structures
Author's notes

Classic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and edge cases, then explain the recursive post-order traversal that returns the node if it matches p or q, otherwise recurses on left and right subtrees. For the iterative approach, describe using a parent pointer map and a set to track ancestors, then compare time and space complexities of both methods.

Pro tip: Emphasize that the recursive solution is elegant but may cause stack overflow for skewed trees, while the iterative solution avoids recursion but uses extra space for the parent map; mention that in an interview, you should discuss trade-offs and possibly implement the one that best fits the constraints.

1. Clarify the problem

Confirm that the tree is not a BST, nodes p and q are guaranteed to exist, and we need the lowest common ancestor (LCA). Discuss edge cases like one node being the ancestor of the other.

2. Recursive approach

Explain the post-order traversal: if current node is null or equals p or q, return current node. Recurse left and right; if both return non-null, current node is LCA; otherwise return the non-null child.

3. Iterative approach

Describe using a stack for DFS to build a parent pointer map (child -> parent). Then, traverse from p to root using a set to store ancestors, and traverse from q upwards until finding a node in the set.

4. Complexity analysis

Compare: Recursive: O(n) time, O(h) space (recursion stack). Iterative: O(n) time, O(n) space (parent map and set). Mention that h can be n in worst case.

5. Discuss trade-offs and choose

Highlight that recursive is simpler and uses less space for balanced trees, but iterative avoids stack overflow for deep trees. Choose based on constraints and mention potential optimizations.

Key Points to Mention

  • Post-order traversal for recursive solution
  • Base cases: null, p, q
  • Parent pointer map and ancestor set for iterative solution
  • Time complexity O(n) for both, space complexity O(h) vs O(n)
  • Handling edge cases: p or q is root, one is ancestor of the other
  • Trade-offs: recursion stack overflow vs extra space in iterative

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

Q3

You have a list of departure flights and a list of return flights, each with a date and price. Find one departure and one return such that the return date is strictly after the departure date and the combined price is minimized. Design an O(n) solution if inputs are pre-sorted by date, and describe an O(n log n) approach otherwise.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one tripped me up more than I expected for what sounds like a two-pointer problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then present the O(n) solution for pre-sorted inputs by scanning and maintaining the minimum departure price seen so far. For unsorted inputs, explain that sorting both lists by date (O(n log n)) enables the same linear scan, and discuss trade-offs like space complexity and stability.

Pro tip: Mention that if the lists are already sorted, you can avoid sorting and achieve O(n) time; otherwise, sorting is necessary. Also, note that you can optimize space by not storing all valid pairs, just the best one.

1. Clarify requirements and edge cases

Ask about input sizes, whether dates are unique, if prices can be negative, and if multiple flights share the same date. Confirm that return date must be strictly after departure date.

2. Outline the O(n) approach for pre-sorted inputs

Use two pointers or a single pass: iterate through return flights in date order, and for each, consider the minimum departure price among all departures with date < return date. Maintain this minimum as you go.

3. Describe the O(n log n) approach for unsorted inputs

Sort both departure and return lists by date (O(n log n)), then apply the same linear scan. Alternatively, sort one list and use binary search on the other, but that would be O(n log n) as well.

4. Analyze complexity and trade-offs

State time and space complexity for both scenarios. Discuss whether sorting in-place is allowed, and if additional space for sorting is acceptable.

5. Test with examples and edge cases

Walk through a simple example, including cases where no valid pair exists, or where the optimal pair uses the earliest departure and latest return.

Key Points to Mention

  • Two-pointer technique or single pass with running minimum
  • Sorting as a preprocessing step for unsorted inputs
  • Time complexity: O(n) if pre-sorted, O(n log n) otherwise
  • Space complexity: O(1) extra if sorting in-place, O(n) if creating new sorted lists
  • Handling of edge cases: no valid pair, equal dates, negative prices
  • Strict inequality: return date must be > departure date

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

Q4

Given a binary tree, count how many nodes have a value equal to the floor of the average of all values in their subtree (including themselves). Provide an O(n) solution.

Algorithms & Data Structures
Author's notes

You need a post-order traversal that returns (sum, count) pairs up the tree, compute the floor average at each node, compare to node value, and accumulate a counter.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the sum and count of nodes in each subtree, allowing you to compute the average and floor in O(1) per node. At each node, after processing children, compute the floor of the average and compare it to the node's value, incrementing a global counter if they match. This yields an O(n) time and O(h) space solution.

Pro tip: Emphasize that the floor operation must be handled carefully for negative numbers (e.g., using integer division or math.floor) and clarify that the average is computed as a floating-point number before flooring. Also, mention that you can avoid floating-point precision issues by comparing sum >= value * count and sum < (value+1) * count for non-negative values, but for general values, use floor division.

1. Clarify the problem and constraints

Confirm that the tree can have negative values, that the average is computed as a real number, and that floor means the greatest integer less than or equal to the average. Ask about input size to justify O(n).

2. Design the recursive function

Define a function that returns a pair (sum, count) for the subtree rooted at a given node. Use post-order traversal to compute these values from children.

3. Compute average and compare

At each node, compute average = sum / count, then floor_avg = floor(average). If node.val == floor_avg, increment a global counter.

4. Handle edge cases and precision

Discuss how to handle negative numbers correctly (e.g., using integer division or math.floor) and avoid floating-point precision issues by using integer arithmetic when possible.

5. Analyze complexity and test

State that the algorithm visits each node once, so time is O(n) and space is O(h) for recursion stack. Walk through a small example to verify correctness.

Key Points to Mention

  • Post-order traversal to compute subtree sums and counts bottom-up.
  • Using a global counter or returning a count from recursion.
  • Handling floor division correctly for negative numbers (e.g., math.floor or integer division).
  • Time complexity O(n) and space complexity O(h) due to recursion stack.
  • Avoiding floating-point precision issues by using integer arithmetic or careful comparison.
  • Edge cases: empty tree, single node, all negative values, large sums causing overflow (use long).

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

Q5

Given a binary grid of 0s and 1s and a starting cell, return all cells 4-directionally connected to the starting cell with the same value. As a follow-up, return the border neighbors: cells adjacent to the component that are not part of it. Discuss BFS vs DFS trade-offs and analyze complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The base problem is a flood fill and I coded BFS without thinking twice.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: the grid is binary, and we need all cells 4-directionally connected to the start with the same value. Then, present a BFS or DFS solution to find the component, and for the follow-up, collect border neighbors by checking adjacent cells outside the component. Finally, discuss trade-offs between BFS and DFS and analyze time and space complexity.

Pro tip: Mention that BFS is generally safer for large grids to avoid recursion depth limits, but DFS with an explicit stack can be equally memory-efficient. Also, note that the border neighbors can be collected during the same traversal by checking neighbors of visited cells.

1. Clarify the problem

Confirm the definition of 'connected' (4-directional) and 'border neighbors' (cells adjacent to the component but not part of it). Ask about grid size, mutability, and whether the start cell is guaranteed to be within bounds.

2. Choose traversal method

Decide between BFS (queue) and DFS (stack or recursion). Explain that both work, but BFS is iterative and avoids recursion limits, while DFS may be simpler to code.

3. Implement component search

Traverse from the start cell, visiting only cells with the same value, and mark them as visited (e.g., by changing value or using a visited set). Collect all component cells.

4. Collect border neighbors

For each cell in the component, check its 4 neighbors. If a neighbor is within bounds, not in the component, and not already added, add it to the border set.

5. Analyze complexity and trade-offs

State that time complexity is O(N) where N is the number of cells in the grid (or O(R*C)), and space complexity is O(N) for the queue/stack and visited set. Compare BFS vs DFS: BFS uses queue, DFS uses stack/recursion; both have same complexity, but BFS may use more memory for wide components, DFS may risk stack overflow.

Key Points to Mention

  • 4-directional connectivity means up, down, left, right only.
  • Use a visited set or modify the grid in-place to avoid revisiting cells.
  • Border neighbors are cells adjacent to the component but with a different value (or outside the component).
  • BFS vs DFS: BFS uses a queue and is iterative; DFS uses a stack or recursion and may be simpler but risks stack overflow.
  • Time complexity: O(R*C) where R and C are grid dimensions, as each cell is visited at most once.
  • Space complexity: O(R*C) in worst case for the queue/stack and visited set.

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

Q6

Given a binary tree, determine whether it represents a valid max-heap. This requires checking both the completeness property (levels filled left to right with no gaps) and the heap-order property (every node is greater than or equal to its children). Implement an O(n) BFS-based solution and explain why DFS alone can fail to catch completeness violations.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The heap-order check is easy to do with any traversal.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the two properties: completeness (all levels filled except possibly the last, which is left-packed) and heap-order (parent >= children). Then propose a single BFS traversal that simultaneously checks for gaps (a node after a null child) and verifies parent-child ordering, achieving O(n) time and O(n) space. Finally, explain why DFS cannot detect completeness violations because it lacks level-order context.

Pro tip: Mention that you can check completeness without extra space by counting nodes and verifying each node's index is less than n, but BFS is more intuitive and still O(n). Also, note that if the tree is not complete, the heap-order check alone is insufficient, so both must be checked together.

1. Clarify definitions and edge cases

Confirm that a valid max-heap requires both completeness and heap-order. Discuss edge cases: empty tree, single node, and trees with duplicate values (heap-order allows equality).

2. Design BFS traversal with completeness check

Use a queue to traverse level by level. Track whether a null child has been seen; if a non-null node appears after a null, the tree is not complete. Also, for each node, ensure its value is >= its children's values.

3. Implement and analyze complexity

Write the BFS code, ensuring each node is enqueued once. Explain that time complexity is O(n) and space complexity is O(n) in the worst case (queue size proportional to number of nodes at a level).

4. Explain why DFS fails for completeness

Illustrate with an example: a tree where a node is missing a left child but has a right child. DFS might visit the right child and not notice the gap, whereas BFS processes level by level and detects the missing left child immediately.

5. Summarize and discuss trade-offs

Conclude that BFS is the natural fit for level-order checks. Mention alternative approaches like index-based recursion (O(n) time, O(h) space) but note BFS is simpler for combined checks.

Key Points to Mention

  • Completeness property: all levels full except last, filled left to right with no gaps.
  • Heap-order property: every node's value >= its children's values (max-heap).
  • BFS traversal naturally processes nodes level by level, enabling detection of gaps.
  • DFS alone cannot detect completeness violations because it may skip over missing left children.
  • Time complexity O(n) and space complexity O(n) for BFS; each node visited once.
  • Edge cases: empty tree, single node, and duplicate values (allowed in heap-order).

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