N-Player High-Card Showdown: Simulate and Score
Data Structure DesignAuto-graded · 21 test casesLast updated
Tests object-style thinking, round-robin dealing, and deterministic simulation of a card game where each round's highest card wins all played cards.
N-Player High-Card Showdown
You are building the engine for a simple card game. A deck of cards is dealt out to n players, and the players compete by flipping cards one at a time. Your job is to simulate the full game and report who won.
The Deck and the Deal
Each card is represented as a list [rank, suit] where rank is an integer. The deck arrives already ordered (the caller controls the order, so tests are deterministic). Cards are dealt round-robin: the card at index i goes to player i % n. If 52 cards do not divide evenly among n players, some players receive one extra card — this is a natural consequence of round-robin dealing and requires no special handling.
Playing the Game
The game proceeds in rounds. In each round, every player who still has cards in their pile flips their top card (the card at the front of their pile). The player whose flipped card has the highest rank wins the round and collects all cards flipped in that round into their personal won-card pile. Players who have already exhausted their pile sit out that round — they neither flip a card nor compete for the cards in play.
The game ends when every player's pile is empty. The winner is the player with the most won cards.
Tie-breaking
- Tie within a round (two or more players flip cards of equal rank): the player with the lower player index wins that round.
- Tie at the end (two or more players finish with the same number of won cards): all tied players are reported as winners.
What to Return
Return a list of two lists:
[winners, won_counts]
winners: a sorted list of the 0-indexed player numbers who share the highest won-card count.won_counts: a list of lengthnwherewon_counts[i]is the total number of cards playeriwon.
Constraints
1 <= n <= 521 <= len(deck) <= 52- Each card is
[rank, suit]whererank >= 0andsuit >= 0. - The deck is pre-ordered; do not shuffle it.
- Won cards are set aside permanently — they never return to a player's active pile.
- A player with no remaining cards is skipped for that round.
- If only one player flips a card in a round (all others are empty), that player wins the single flipped card uncontested.
Examples
Example 1
Input: simulate_card_game([[1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0]], 2)
Output: [[1], [0, 6]]
Example 2
Input: simulate_card_game([[1, 0], [1, 1], [2, 0], [2, 1]], 2)
Output: [[0], [4, 0]]
Example 3
Input: simulate_card_game([[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], 3)
Output: [[2], [0, 2, 3]]
Hints
Hint 1
Deal the deck by iterating once and appending each card to pile i % n — the uneven distribution falls out automatically.
Hint 2
Use a collections.deque (or list with pop(0)) for each player's pile so you can efficiently pull from the front each round. Collect (player_index, card) pairs for every player who still has cards, then pick the winner with max() using (rank, -player_index) as the key.
Hint 3
After the game loop, find the maximum value in your won-counts list, then collect every player index that matches it — that handles both the single-winner and the multi-winner tie cases in one pass.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key observations
- Dealing is a single pass: card at position
igoes topiles[i % n]. Uneven distribution is automatic — no special case needed. - Each round is stateless: pull the front card from every non-empty pile, find the max, credit that player with
len(revealed)cards. - Termination is guaranteed because every round consumes exactly as many cards as there are active players, and no card is ever returned to a pile.
- Tie-breaking within a round is handled by the sort key
(rank, -player_index):max()with a negative index naturally prefers the lower-numbered player when ranks are equal. - End-of-game tie is resolved by a single linear scan: collect every player whose count matches the maximum.
Why it is correct
- Every card dealt ends up in exactly one player's won pile (or is the single card in a one-player round). No card is lost or double-counted.
- Players who exhaust their pile are simply skipped in subsequent rounds — they cannot win more cards, but they also cannot lose cards they no longer have.
- The round-robin deal mirrors the stated rule precisely:
deck[0]to player 0,deck[1]to player 1, …,deck[n]back to player 0, etc.
Worked examples
| Call | Piles after deal | Play | Result |
|---|---|---|---|
deck=[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0]], n=2 | P0: [1,3,5], P1: [2,4,6] | R1: 1v2→P1; R2: 3v4→P1; R3: 5v6→P1 | P1 wins 6 cards |
deck=[[1,0],[1,1],[2,0],[2,1]], n=2 | P0: [1,2], P1: [1,2] | R1: 1v1→P0 (lower index); R2: 2v2→P0 | P0 wins 4 cards |
deck=[[1,0],[2,0],[3,0],[4,0],[5,0]], n=3 | P0: [1,4], P1: [2,5], P2: [3] | R1: 1v2v3→P2; R2: 4v5→P1 | won=[0,2,3], winner=P2 |
Complexity
- Time: O(d + r · n) where d = deck size and r = number of rounds. In the worst case (n = 1) this is O(d). In general r ≤ d / 1 = d, giving O(d · n) which is at most O(52 · 52) = O(2704) — effectively constant for the given constraints.
- Space: O(d) for the piles plus O(n) for the won-count array.
Reference solution
from typing import List
def simulate_card_game(deck: List[List[int]], n: int) -> List[List[int]]:
if n <= 0:
raise ValueError("n must be positive")
# Deal round-robin into each player's pile (front = index 0)
piles = [[] for _ in range(n)]
for i, card in enumerate(deck):
piles[i % n].append(card)
# Track won-card counts per player
won = [0] * n
# Play until all piles are empty
while any(piles):
# Collect one card from each player who still has cards
revealed = []
for player, pile in enumerate(piles):
if pile:
revealed.append((player, pile.pop(0)))
if not revealed:
break
# Highest rank wins; ties go to the lower player index
# key: (rank, -player_index) so max() picks lowest index on rank tie
round_winner = max(revealed, key=lambda item: (item[1][0], -item[0]))[0]
won[round_winner] += len(revealed)
# Determine winner(s): all players sharing the highest count
highest = max(won)
winners = [player for player, count in enumerate(won) if count == highest]
return [winners, won]