PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Simulate a two-player card war game states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • EvenUp
  • Coding & Algorithms
  • Software Engineer

Simulate a two-player card war game

Company: EvenUp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement a function to simulate a two-player card game. Each player has a queue of card ranks (top at index 0), with ranks 2–14 where 11–14 represent J, Q, K, A; suits are ignored. In each round, both players draw their top card and compare ranks. The higher rank wins the round; the winner places all cards on the table to the bottom of their deck in the order revealed (winner’s card before opponent’s for each reveal). If the two cards tie, each player must draw exactly three additional cards; if a player cannot provide three additional cards, that player immediately loses. Otherwise, compare the third additional card from each player; the higher rank wins and takes all cards on the table. Continue until one player has no cards. To prevent infinite cycles, either stop if a previously seen (p1_deck, p2_deck) state repeats or after a large round cap (e.g., 1,000, 000), and in that case return a draw. Return the winner (1, 2, or "draw") and the total number of rounds simulated. Aim for O(total cards moved) time and O(n) space, where n is the total number of cards.

Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Simulate a two-player card war game states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Simulate a two-player card game ("War"). Each player owns a queue of card ranks with the top of the deck at index 0. Ranks are integers 2-14 (11-14 mean J, Q, K, A); suits are ignored. In each round both players draw their top card and compare ranks: - The higher rank wins the round. The winner takes every card currently on the table and places them at the bottom of their own deck in the order the cards were revealed, with the winner's card placed before the opponent's card for each reveal. - If the two cards tie, each player must draw exactly THREE additional cards. If a player cannot supply three additional cards, that player immediately loses. Otherwise compare the THIRD additional card from each player; the higher third card wins and takes all cards on the table (same ordering rule). If the third cards also tie, repeat the tie procedure. Play continues until one player has no cards. To prevent infinite cycles, stop and return a draw if a previously seen (p1_deck, p2_deck) state repeats, or after a large round cap (default 1,000,000). Return a tuple (winner, rounds) where winner is 1, 2, or the string "draw", and rounds is the total number of rounds simulated. Target O(total cards moved) time and O(n) space where n is the total number of cards.

Constraints

  • 2 <= each card rank <= 14 (11-14 = J, Q, K, A)
  • 0 <= len(p1), len(p2)
  • Top of each deck is at index 0
  • On a win, table cards go to the bottom of the winner's deck in reveal order, winner's card before opponent's for each reveal
  • On a tie each player draws exactly 3 more cards; failure to supply 3 means immediate loss
  • Return a draw if a (p1_deck, p2_deck) state repeats or the round cap (default 1,000,000) is reached

Examples

Input: ([2, 5], [3, 4])

Expected Output: (1, 4)

Explanation: P2 wins R1 (2<3) taking [3,2]; P1 then wins R2 (5>4), R3 (5>3), R4 (4>2). P2 runs out -> player 1 wins after 4 rounds.

Input: ([14], [2])

Expected Output: (1, 1)

Explanation: Ace (14) beats 2 in one round; player 2 has no cards left.

Input: ([], [])

Expected Output: ('draw', 0)

Explanation: Both decks are empty before any round is played, so it is a draw with 0 rounds.

Input: ([5], [5])

Expected Output: (2, 1)

Explanation: The single cards tie; the tie rule requires each to draw 3 more, but player 1 has none left, so player 1 immediately loses -> winner is player 2.

Input: ([5, 6, 7, 8], [5, 2, 3, 4])

Expected Output: (1, 1)

Explanation: 5 vs 5 ties; both draw 3 more (6,7,8 vs 2,3,4). The third drawn cards 8 vs 4 give player 1 the round, taking all 8 cards; player 2 is emptied in one round.

Input: ([2], [3])

Expected Output: (2, 1)

Explanation: 3 beats 2 in a single round; player 1 has no cards left.

Input: ([3, 3], [3, 3])

Expected Output: (2, 1)

Explanation: First cards 3 vs 3 tie; each needs 3 more cards but only has 1 left, so player 1 (checked first) cannot supply them and immediately loses -> player 2 wins.

Hints

  1. Use deques so popleft (draw top) and extend (append to bottom) are both O(1).
  2. Keep a 'table' list of cards revealed this round in reveal order: p1, p2, p1, p2, .... On a win, append them to the winner's deck; if player 2 wins, swap each (p1,p2) pair so the winner's card comes first.
  3. A tie is just a loop: keep drawing 3 cards each (failing immediately if a player can't) and re-compare the new third cards until one side is strictly higher.
  4. For infinite-cycle safety, store seen (tuple(p1), tuple(p2)) states in a set (or cap the rounds) and return "draw" on repeat.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.