PythonStudio
Interview Problems

Deck of Cards

easy · a finite set with a seeded shuffle

Brief-reported interview attribution (unverified): Amazon, Microsoft

How to approach an LLD round

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.

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])

Starter code

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
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler