MedianFinder

Python data engineering interview problem. Difficulty: advanced. Pattern: Heaps. About 18 minutes. Part of the Pro drill bank.

Stream integers and report the running median. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A live dashboard needs the middle value after every new measurement, even though the stream does not arrive sorted. Implement a MedianFinder class. addNum adds one number and findMedian returns the middle number, or the average of the two middle numbers as a float when the count is even. Write class MedianFinder. After adding 1 and 2, the middle is (1 + 2) / 2 = 1.5; after adding 3, the median is 2. The starter file declares the class and harness calls. Keep the class name, method names and print lines, and fill in the methods.

Requirements

  • Add numbers without losing earlier values.
  • Return a float average for an even count.
  • Keep median queries correct after each insertion.

Constraints

  • The class receives integers and may be queried many times.
  • The starter harness supplies the method calls and expects addNum and findMedian.

Examples

Input: add 1,2; findMedian; add 3; findMedian Output: 1.5 then 2 Even then odd counts.

Topics: lakebench, python, median, two heaps.

More Python interview questions · All interview problems · Learn data engineering

advanced

MedianFinder

Interview-style drill: Stream integers and report the running median.

Implement class `MedianFinder` with `addNum(num)` and `findMedian()`. For even count, return the average of the two middle values as a float. Keep the harness prints.