Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Card Game War Implementation

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Card Game War Implementation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Implement a simulator for a two-player War game used in a Workiva collaborative review prototype. Each player's deck is represented from top to bottom, and the function must return the winning player.

On each round, both players draw their top card. The player with the higher rank wins all cards played in that battle and places them at the bottom of their deck in play order. If the ranks are equal, each player must place three face-down cards followed by one face-up card. Compare the new face-up cards, repeating the process if they tie again. A player who cannot provide all three face-down cards and a face-up card loses immediately.

Formal Specification

Implement war_winner(deck1, deck2), where deck1 and deck2 are lists of integers ordered from top to bottom. Return integer 1 if player 1 wins, or 2 if player 2 wins. Card integers represent ranks, so duplicate values are allowed. Inputs are guaranteed to produce a terminating game.

Constraints

  • 1 <= len(deck1), len(deck2) <= 26
  • 0 <= deck1[i], deck2[i] <= 13
  • Card values represent ranks, so duplicate values are allowed
  • Inputs are guaranteed to produce a terminating game

Function Signature

def war_winner(deck1, deck2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output