KatInterview

N-Player High-Card Showdown: Simulate and Score

Data Structure DesignAuto-graded · 21 test cases
HardAuto-gradedData Structure Designsimulationqueuesgame-design

Last 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 length n where won_counts[i] is the total number of cards player i won.

Constraints

  • 1 <= n <= 52
  • 1 <= len(deck) <= 52
  • Each card is [rank, suit] where rank >= 0 and suit >= 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.