Running Median of Gift Values During a LIVE Stream

Last updated: November 29, 2026

Quick Overview

Gifts arrive one by one during a LIVE stream, each with a coin value. After each gift, report the median value of all gifts so far, so the creator dashboard can show a robust typical gift size.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Gifts arrive one by one during a LIVE stream, each with a coin value. After each gift, report the median value of all gifts so far, so the creator dashboard can show a robust typical gift size.

TikTok LIVE dashboards prefer medians to averages because a single huge gift skews the mean. This technical screen checks the two-heap technique and careful rebalancing.

What the Interviewer Expects
  • Uses a max-heap for the lower half and a min-heap for the upper half
  • Keeps sizes balanced within one
  • Returns the correct median for odd and even counts
  • Explains why sorting after each insert is too slow
  • Discusses approximate medians at very high volume
Key Topics to Cover
heaps
two heaps
streaming median
order statistics
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 10 minutes?
  • At millions of gifts per second across rooms, what approximate structure would you use?
  • How would you report the 90th percentile instead?
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
  • Input: sequence of positive ints.
  • Output: list of medians after each insert; for an even count, the average of the two middle values.
  • O(log n) per insert....
High-Level Design

Two heaps: low is a max-heap (stored as negatives in Python) holding the smaller half; high is a min-heap holding the larger half. Invariant: every value in low is at most every value in high,...


Submit Your Answer
Markdown supported