Simulate a two-player card war game
Company: EvenUp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
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.
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
- Use deques so popleft (draw top) and extend (append to bottom) are both O(1).
- 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.
- 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.
- For infinite-cycle safety, store seen (tuple(p1), tuple(p2)) states in a set (or cap the rounds) and return "draw" on repeat.