← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Meta SWE coding round with a median-from-stream problem. Pretty standard algorithmic interview but the real-time constraint pushed it beyond a naive sorted array approach.

Questions Asked (1)

Q1

Design a data structure that supports adding numbers from a stream one at a time and returning the current median after each insertion. How do you keep this efficient as the stream grows?

Algorithms & Data Structures
Author's notes

My first instinct was binary search insertion into a sorted array which is what the prompt basically hints at, but then they asked about the time complexity and I had to admit that shifting elements on insert is O(n).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., data types, stream size, memory limits) and then propose a two-heap solution: a max-heap for the lower half and a min-heap for the upper half. Explain how to maintain balance and compute the median in O(1) time after each insertion, with O(log n) insertion time.

Pro tip: Mention that you would handle edge cases like even/odd number of elements and discuss potential optimizations such as using a self-balancing BST if the stream is extremely large and heaps are not feasible due to memory constraints.

1. Clarify Requirements

Ask about the nature of the stream (e.g., integers, floats), expected size, and whether memory is a constraint. Confirm that the median should be returned after each insertion.

2. Propose Data Structure

Suggest using two heaps: a max-heap for the smaller half and a min-heap for the larger half. Explain that this allows O(1) median retrieval and O(log n) insertion.

3. Detail Insertion Logic

Describe the algorithm: add the new number to the appropriate heap, then rebalance so that the size difference between heaps is at most 1. Ensure the max-heap's top is <= min-heap's top.

4. Compute Median

Explain that if heaps are equal size, median is the average of the two tops; otherwise, it's the top of the larger heap.

5. Analyze Complexity and Edge Cases

State time complexity: O(log n) per insertion, O(1) for median. Discuss edge cases: empty stream, single element, duplicates, and potential overflow when averaging.

Key Points to Mention

  • Two-heap approach: max-heap for lower half, min-heap for upper half
  • Balancing condition: size difference <= 1 and max-heap top <= min-heap top
  • Time complexity: O(log n) insertion, O(1) median retrieval
  • Space complexity: O(n) for storing all elements
  • Handling even/odd number of elements for median calculation
  • Alternative approaches (e.g., self-balancing BST, sorted list) and trade-offs

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