What they’re really asking
If you only remember one thing, say this in the first minute: “I’ll keep this small: a card is an immutable suit-and-rank pair, and a Deck class has shuffle, deal, sort and view, each doing one thing. The random source is passed in, so a test can seed it and get the same shuffle every time. Dealing too many cards raises a clear error, and view returns a copy so nobody can change the deck from outside. I’ll write the shuffle myself so it’s unbiased, then add tests, then take your follow-ups.”
The rest of this page explains each part of that answer.
This is an object-oriented design screen where the design is small and the time is short. You write a deck of playing cards with a few methods. Nothing about it is hard, and that is the point.
What makes it different from a design discussion is that nobody wants a class diagram. Many candidates hear “object-oriented design” and start building a hierarchy of abstract classes. The interviewer is grading something else: sensible types, methods that do one thing, randomness you can test, clean errors, and finishing on time. Every minute spent on a structure the problem doesn’t need is a minute not spent on tests.
The Scale AI candidate’s round was “to implement methods based on a deck of cards, shuffling, dealing, sorting and viewing the cards”, and “Speed was definitely important here as it was time constrained” (TechPrep). That candidate was later rejected after the final round; the report gives no result for this screen.
Clarify first
- Card representation: a suit and a rank. Aces high or low? Assume ranks 2–14, with 14 as ace.
- Shuffle: must it be reproducible in tests? Assume yes, so the random source is injected (passed in).
- Deal: to one hand or round-robin to several players? What if there aren’t enough cards? Assume raise an error.
- Sort: by suit then rank, or by rank then suit? Make the sort key a parameter.
- Multiple decks, jokers: likely follow-ups; leave room.
Say it like this: “I’ll pass the random generator in, so tests can fix the seed. Everything else I’ll keep as simple as possible and tell you where I left room for jokers and multiple decks.”
Answer
Words you need first
- Injected randomness means the class receives a random-number generator as an argument instead of calling the
global
randommodule. A test passesrandom.Random(42); production passes nothing and gets a fresh one. - Fisher–Yates is the standard unbiased shuffle: walk from the last card down, and swap each card with a random card at or before it. Every one of the possible orderings is equally likely.
The code
SUITS = ("clubs", "diamonds", "hearts", "spades")
RANKS = tuple(range(2, 15)) # 11-14 = J, Q, K, A
class Deck:
def __init__(self, rng=None, n_decks=1):
self.cards = [(s, r) for _ in range(n_decks) for s in SUITS for r in RANKS]
self.rng = rng or random.Random()
def shuffle(self): # Fisher-Yates, with an injected RNG
c = self.cards
for i in range(len(c) - 1, 0, -1):
j = self.rng.randint(0, i)
c[i], c[j] = c[j], c[i]
def deal(self, n=1):
if n > len(self.cards):
raise ValueError("not enough cards")
hand, self.cards = self.cards[:n], self.cards[n:]
return hand
def sort(self, key=lambda card: (SUITS.index(card[0]), card[1])):
self.cards.sort(key=key)
def view(self):
return list(self.cards) # a copy: callers can't mutate the deck
The choices to say out loud
Each line of the class answers a question the interviewer may ask. Name the choice as you write it.
| Choice | Why | What to say in the interview |
|---|---|---|
Cards are tuples (suit, rank) | immutable and cheap; a frozen dataclass if the interviewer wants named fields | ”A card never changes, so it shouldn’t be mutable.” |
rng is a constructor argument | two decks with the same seed shuffle the same way, so shuffle is testable | ”Randomness is a dependency, like a clock. I inject it.” |
Fisher–Yates with randint(0, i) | randint includes both ends, so a card can stay where it is; that is required for fairness | ”Swapping each card with any position looks fine but is biased.” |
deal checks before slicing | dealing too many raises instead of returning a short hand | ”Fail loudly on bad input.” |
sort takes a key | suit-then-rank or rank-then-suit without a second method | ”One method, the order as a parameter.” |
view returns a copy | a caller can’t change the deck by editing the list it got | ”Don’t leak internal state.” |
n_decks argument | costs one argument now and answers the multi-deck follow-up | ”Cheap room for the obvious next question.” |
Writing Fisher–Yates yourself shows you know why the naive shuffle is biased; random.shuffle is fine if they
don’t ask.
The arithmetic behind the numbers (for a Staff-level follow-up)
Why the naive shuffle is biased: for each of n positions it picks one of n positions, so it has n^n equally likely runs. A deck of n cards has n! orderings. For an unbiased shuffle every ordering must come from the same number of runs, so n^n would have to be a multiple of n!. For n = 3: 3^3 = 27 runs and 3! = 6 orderings; 27 is not a multiple of 6, so some orderings must come up more often than others. Fisher–Yates makes n × (n − 1) × … × 2 = n! runs, exactly one per ordering.
Tests and follow-ups
Tests: a deal removes cards and they don’t reappear, dealing too many raises, sort is idempotent (sorting twice gives the same result as sorting once), and two seeded decks shuffle identically. They passed when run on 2026-10-12.
Likely follow-ups:
- A
Handclass and a game rule, such as blackjack scoring, where an ace counts as 1 or 11. The rule belongs in the game, not inDeck. - Jokers: a third kind of card with no suit or rank. That is the moment a
Cardclass beats tuples. - Dealing to several players: round-robin, one card at a time, with the same “not enough cards” check.
Say it like this: “Jokers are where I’d switch from tuples to a small Card class. I didn’t do it earlier because
nothing needed it.”
Senior vs Staff
Deck of cards
Senior
- Small clean API, injected randomness, correct shuffle, errors on bad input, tests in the time limit.
Staff
- Same, faster, plus a clear line on what belongs in Deck versus a game's rules, and no speculative class hierarchy.
Small-team version
Low direct relevance: no small company needs a deck of cards. The habit that transfers is injecting randomness and time so code is testable.
That matters more in AI work than in most backend work, because so much of it is random on purpose:
- Sampling from a language model is random unless you fix the settings, so a test that compares outputs needs a fixed seed or a fake model.
- A/B test buckets must be reproducible, or a user flips between variants.
- Shuffling an evaluation set (the fixed list of test questions you score a model on) must give the same split each run, or scores aren’t comparable.
Fix the seed in tests, and log it in production runs you may need to reproduce.
Pitfalls
- A biased shuffle (
j = randint(0, n - 1)for every i). It looks random and fails a fairness test. - Module-level
randomcalls. Tests become flaky because nobody controls the seed. - Returning the internal list from
view. Callers can then change the deck behind its back. - Spending ten minutes on an inheritance hierarchy. The time limit can’t afford it, and nothing needs it.
Go deeper
- Family walkthrough: Multi-part practical coding, for pacing and keeping each rule in one place.
- Another small-object screen in the same family: the resumable iterator.