KatInterview

Lottery Bucket with Constant-Time Operations and Duplicates

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-tablearrayrandomization

Last updated

Build a data structure supporting O(1) add, remove, and uniform random selection where duplicate entries are treated as separate items.

You're implementing a lottery bucket system. Players can drop entries (represented as short strings) into the bucket, and the same entry identifier can appear multiple times — each occurrence counts as a separate, independent item in the bucket.

Your data structure must support three operations:

class LotteryBucket:
    def add(self, entry: str) -> bool: ...
    def extract(self, entry: str) -> bool: ...
    def pick_random(self) -> str: ...
  • add(entry) places one additional item with this identifier into the bucket. Returns True if this is the first time this particular identifier has been added to the bucket, False if items with this identifier were already present.

  • extract(entry) attempts to remove exactly one item with this identifier from the bucket. Returns True if an item was successfully removed, False if no items with this identifier exist in the bucket.

  • pick_random() returns the identifier of one randomly selected item from the bucket, where every individual item has equal probability of selection. For instance, if the bucket contains 5 total items and 2 of them share identifier "X", then "X" should be selected with probability 2/5.

All three operations must achieve average O(1) time complexity.

Example

bucket = LotteryBucket()
bucket.add("TICKET-A")      # True  (first occurrence of TICKET-A)
bucket.add("TICKET-A")      # False (TICKET-A already exists)
bucket.add("TICKET-B")      # True
bucket.extract("TICKET-A")  # True  -> one TICKET-A removed, one remains
bucket.extract("TICKET-C")  # False -> not present
bucket.pick_random()        # "TICKET-A" or "TICKET-B", each with 50% probability

Constraints

  • The system will process up to 10^5 total operations across all three methods.
  • Entry identifiers are short strings. The same identifier may exist in multiple copies simultaneously.
  • pick_random() will only be called when at least one item exists in the bucket.
  • For complexity analysis, treat string hashing and comparison as O(1) operations.

Design Considerations

Your solution should explain:

  • How you ensure pick_random() gives every physical item equal selection probability
  • How you remove a specific item without scanning or reorganizing the entire collection
  • Why your approach correctly handles edge cases like removing duplicates or extracting the most recently added item

Hints

Hint 1

Store every individual item in a contiguous structure so you can select by random index

Hint 2

Use a lookup map to track where each identifier appears, enabling fast removal without scanning

Hint 3

When removing an item creates a gap, fill it by moving another item — but remember to update that moved item's location in your lookup map