Deck of Cards
easy · a finite set with a seeded shuffle
Brief-reported interview attribution (unverified): Amazon, Microsoft
How to approach an LLD roundRequirements
- Functional: a standard 52-card deck in canonical order, a seeded shuffle (reproducible), deal from the top, report remaining cards.
- Non-functional: the deck must never contain a duplicate; a shuffle must be reproducible from its seed (tests check properties, never an exact order);
cards()returns a copy. - Out of scope: game rules (poker, blackjack), jokers.
Statement
Implement a standard 52-card deck. Ranks are 2–10, J, Q, K, A;
suits are Hearts, Diamonds, Clubs, Spades. A fresh deck is in canonical order:
Hearts 2 through A, then Diamonds, then Clubs, then Spades. shuffle shuffles
the remaining cards; it accepts an optional seed, and the constructor accepts one
too — a seeded shuffle is reproducible, which is how the tests pin it down.
deal(n) takes n cards from the top (fewer if the deck runs out).
class Deck:
SUITS = ("Hearts", "Diamonds", "Clubs", "Spades")
RANKS = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
def __init__(self, seed=None):
def shuffle(self, seed=None):
# shuffles the remaining cards (with a new seed if one is given)
def deal(self, n=1):
# -> list of (rank, suit) cards from the top; [] when the deck is empty
def remaining(self):
# -> number of cards left
def cards(self):
# -> list of the remaining cards in current order (a copy)Rules. Cards are (rank, suit) tuples, e.g. ("A", "Spades"). The deck never
contains a duplicate card. cards() returns a copy, so callers cannot mutate the
deck. Note: this is the one problem where seeded randomness is allowed — the seed
makes the shuffle deterministic, and the hidden tests check properties and
reproducibility, never an exact shuffled order.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Card (rank + suit; SRP — a value object). Deck (cards + seeded shuffle + deal). No pattern is forced; the split (card vs deck) is the point. The seed is dependency injection for randomness.
Public demo
# Public demo: run after implementing the class API above.
d = Deck()
canonical = [(rank, suit) for suit in ('Hearts', 'Diamonds', 'Clubs', 'Spades') for rank in ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')]
print(d.cards())
print(d.remaining())
print(d.cards()[0])
print(d.cards()[-1])Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
A deck is used by one game at a time — no shared state beyond the deck. In a server version, one deck per table with a lock per deck around deal/shuffle.
Trade-offs & extensions
The canonical order is suit-major (Hearts, then Diamonds, Clubs, Spades) — a convention, not a rule. Extending: a Hand class and game rules (poker hand evaluation) on top of the deck.
Starter code
import random
class Card:
def __init__(self, rank, suit):
"""Initialize the state described in the problem."""
pass
def as_tuple(self):
"""Implement the return value and rules in the API contract."""
pass
class Deck:
def __init__(self, seed=None):
"""Initialize the state described in the problem."""
pass
def shuffle(self, seed=None):
"""shuffles the remaining cards (with a new seed if one is given)"""
pass
def deal(self, n=1):
"""-> list of (rank, suit) cards from the top; [] when the deck is empty"""
pass
def remaining(self):
"""-> number of cards left"""
pass
def cards(self):
"""-> list of the remaining cards in current order (a copy)"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter