Running Median of Basket Sizes in a Checkout Stream

Last updated: November 29, 2026

Quick Overview

Checkout events arrive one at a time with the number of items in each basket. Support adding a basket size and returning the median of all sizes seen so far, each in logarithmic time or better.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Checkout events arrive one at a time with the number of items in each basket. Support adding a basket size and returning the median of all sizes seen so far, each in logarithmic time or better.

Store operations dashboards at Walmart track median basket size per lane in real time, because the mean is skewed by huge stock-up trips. The Walmart Labs onsite interviewer checks the two-heap technique, the balance invariant, and how you would bound memory in a real stream.

What the Interviewer Expects
  • Split the data into a max-heap of the lower half and a min-heap of the upper half
  • Maintain a size invariant after each insert
  • Return the middle element or the average of the two middles
  • Explain why sorting on each query is too slow
  • Discuss windowed or approximate variants for unbounded streams
Key Topics to Cover
heaps
streaming
order statistics
invariants
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you compute the median over only the last hour of checkouts?
  • If basket sizes are integers between 1 and 500, can you do better than heaps?
  • How would you compute the 95th percentile instead?
  • How would you merge medians from 4,700 stores into one company-wide value?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

add(x) takes an integer basket size. median() returns the median of everything added so far as a float, or raises if empty. Both should be fast for millions of events. Duplicates are common, since...

High-Level Design

Keep the smaller half in low, a max-heap simulated by negating values, and the larger half in high, a min-heap. Invariants: every value in low is at most every value in high, and len(low) equals...


Submit Your Answer
Markdown supported