As reported: Walmart Software Engineer III (Java with DevOps) technical round 1 (Bangalore, 2025): a variation of LeetCode 295 Find Median from Data Stream — approach, Java code and complexities; the interviewer also asked how a heap works internally.
Implement MedianFinder:
addNum(num)adds an integer from the stream.findMedian()returns the median of all numbers added so far (mean of the two middle values when the count is even).
This is LeetCode 295 — Find Median from Data Stream.
Example
["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"]
[[],[1],[2],[],[3],[]]
-> [null,null,null,1.50000,null,2.00000]
Follow-ups
- How does a heap work internally?
Constraints: -10^5 ≤ num ≤ 10^5; findMedian is called only after at least one addNum; at most 5·10^4 calls.
Code runner format: two lines — the method names, then their arguments (LeetCode's format). Implement the MedianFinder class; the harness prints the results, medians with 5 decimals.