Maintain a Top Gifters Leaderboard for a LIVE Stream
Last updated: November 29, 2026
Quick Overview
During a TikTok LIVE, viewers send gifts worth coins. Design `add_gift(user, coins)`, `top(k)`, `rank(user)` and `reset(user)` so the on-screen leaderboard can be refreshed constantly; ties are ordered by user id.
TikTok
November 29, 20260
0
0 solved
During a TikTok LIVE, viewers send gifts worth coins. Design `add_gift(user, coins)`, `top(k)`, `rank(user)` and `reset(user)` so the on-screen leaderboard can be refreshed constantly; ties are ordered by user id.
The top gifter ranking is visible in every LIVE room and changes many times per second in big streams, so this technical screen question is about picking the right ordered structure. The interviewer wants you to compare a heap, a sorted list and a balanced tree or skip list, and to justify which operations you make cheap.
What the Interviewer Expects
- Identifies the read pattern (top k very often, rank sometimes) before choosing a structure
- Keeps a score map plus an ordered index and keeps them consistent
- Correct tie-break and correct removal of the old entry on update
- States per-operation costs honestly, including O(n) list insertion
- Mentions Redis sorted sets or a skip list as the production equivalent
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Why not a heap, and what would top(k) and rank cost with one?
- A mega-stream has 2 million gifters; what changes?
- How would you shard the leaderboard if gifts arrive at multiple servers?
- How would you show each viewer their own rank cheaply?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Scores only grow except on explicit reset. top(k) returns (user, coins) pairs best first; ties broken by user id ascending. rank(user) is 1-based or None for unknown users. Assume a room has up ...
High-Level Design
Keep score[user] plus order, a list sorted by (-score, user). That key makes the natural sort order the leaderboard order, including the tie-break. On a gift, I binary-search the user's old tupl...