Pick a Random Card — Randomized
Build a dealer that returns a card per call from a small deck, every card equally likely each time, with dealt cards returned to the deck.
Randomized · Easy
The problem
A card game deals one card at a time from a small deck. Build a class over a list of distinct cards. Each call to draw() returns one card chosen uniformly at random. The card goes back into the deck every time, so every call is independent and every card is equally likely on every call.
The story
A card game deals one card at a time from a tiny deck of 1 to 4 different cards, each numbered from -9 to 9. For every deal, each card must be equally likely, and the dealt card goes straight back into the deck, so the next deal is a fresh draw from the whole deck. Build the dealer so that each call picks one card at random.
What to return
- A test is a sequence of calls on one Solution; the result is the list of outputs, one per call (the constructor gives nothing and receives the list of cards).
- draw() returns one card of the deck. Each card is equally likely and calls are independent.
- The checker verifies that every result is a card from the deck and (on long tests of 140 or more calls) that each card was drawn within 80% of its fair share of the calls.
Constraints
- the sequence has 2 to 150 calls; the first is the constructor Solution(cards)
- the deck has 1 to 4 distinct cards with values in -9..9
- every other call is draw() with no arguments
Example
- Input
Solution([2,-1,3]), draw(), draw(), draw()- Output
—