FastPrepWar Card Game Winner

War Card Game Winner

EvenUp logoEvenUp● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Two players hold card queues from top to bottom. In each battle, both draw their top card. The higher card wins the entire battle pile and appends it to the bottom of that player's queue in draw order, with player one's card before player two's card at each draw.

On a tie, each player draws three more cards; the third new card decides the battle. Ties repeat the same rule. A player who cannot supply all three tie cards loses immediately. Return 1 or 2 for the winner.

Function

warWinner(playerOne: int[], playerTwo: int[]) → int

Examples

Example 1

playerOne = [5,1]playerTwo = [3,2]return = 1

Player one eventually collects every card.

Constraints

  • 1 <= playerOne.length, playerTwo.length <= 100.
  • Card values are from 1 through 13.
  • Tests do not exhaust both players simultaneously during a tie and terminate within 100000 comparisons.

More EvenUp problems

See EvenUp hiring insights
public int warWinner(int[] playerOne, int[] playerTwo) {
    // Simulate the queues and return 1 or 2.
}
playerOne[5,1]
playerTwo[3,2]
expected1
Checking account…