Ticket Pool for a Douyin Live Lucky Draw With O(1) Operations

Last updated: November 29, 2026

Quick Overview

Viewers earn draw tickets during a Douyin live stream and some tickets are revoked for abuse. Design a multiset supporting add(user), remove(user) of one ticket, and draw() returning a ticket uniformly at random, each in average O(1).

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

0

0

0 solved


Viewers earn draw tickets during a Douyin live stream and some tickets are revoked for abuse. Design a multiset supporting add(user), remove(user) of one ticket, and draw() returning a ticket uniformly at random, each in average O(1).

Douyin live rooms run lucky-draw events where viewers who comment or gift earn entries, and risk control can revoke entries in real time. The interviewer is checking whether you can combine an array with a hash index and keep both consistent through swap-with-last removal, especially when the same user holds many tickets.

What the Interviewer Expects
  • Combine a dense array for O(1) random access with a hash of positions
  • Remove via swap-with-last to stay O(1)
  • Handle duplicates by mapping each user to a set of indexes
  • Keep the index map correct when the removed slot is the last one
  • Explain why the draw is uniform over tickets, not users
Key Topics to Cover
hash map
randomized data structure
swap with last
multiset
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 make the draw uniform over distinct users instead of tickets?
  • How would you support weighted tickets where gifts count more than comments?
  • How would you make draws auditable and reproducible for regulators?
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(uid) appends a ticket. remove(uid) revokes one ticket held by that user and returns whether one existed. draw() returns a uniformly random ticket's owner, or None if empty. All three should ...

High-Level Design

Store tickets in a flat list so random.choice is uniform and O(1). Keep pos[uid], the set of list indexes holding that user's tickets. To remove, take any index i from the user's set, move the l...


Submit Your Answer
Markdown supported