InterviewDryRun
Coding interview · Asked at senior and staff level

Find Median from Data Stream

How the interviewer asks it

“Numbers arrive one at a time. After each one, report the median of all numbers seen so far.”
Sample test input
1
2
3
Expected output
1.0
1.5
2.0

In the practice room your code reads the input from standard input, and Run checks it against tests like this one. Try it now in your browser, in Python or JavaScript.

Typed drills are free and unlimited: the AI scores your answer against the outline below.

What the interviewer is looking for

  • Two heaps: a max-heap for the lower half and a min-heap for the upper half.
  • Clear invariants: sizes differ by at most one, and every value in the lower heap is ≤ every value in the upper heap.
  • Correct median for odd and even counts.
  • O(log n) per insert and O(1) per median.

How to structure your answer

  1. 01

    Baseline

    Keep a sorted list and insert in place: O(n) per insert. Say why it is too slow for a long stream.

  2. 02

    Two heaps

    Push into the lower max-heap, move its top to the upper heap, then rebalance so the lower heap is never smaller.

  3. 03

    Median

    If the heaps are the same size, average the two tops; otherwise it is the lower heap's top.

  4. 04

    Test

    Stream 5, 15, 1, 3 gives medians 5, 10, 5, 4.

  5. 05

    Complexity

    O(log n) per number, O(n) space.

Best answers from candidates

No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.

Answers are shared by candidates, without names, after a privacy check. Add yours

Common mistakes

  • Forgetting that most languages only provide a min-heap, and negating values wrongly.
  • Rebalancing by size but not by value, which breaks the ordering invariant.
  • Integer division when averaging the two middle values.

Follow-up questions to expect

  • Report the median of only the last k numbers (a sliding window).
  • All numbers are between 0 and 100. Can you do better?

Now answer it out loud

Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.

Practice with Max

Free: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.

More coding interview questions

Preparing for this round? Read how to prepare for a coding interview: a four-week DSA plan.