FastPrepFour-Player Matching Card Game

Four-Player Matching Card Game

Scale AI logoScale AI● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Cards are encoded as rank-suit, where rank is an integer and suit is a nonempty uppercase token. deckOrder is the supplied deck from top to bottom.

  1. Deal handSize cards to each of four players in round-robin order, then sort every hand by rank and suit.
  2. Draw the next deck card as the current card.
  3. Players take turns 0,1,2,3. A player plays the first card in their sorted hand whose rank or suit matches the current card. That card becomes current.
  4. If no card matches, draw the next deck card into the hand, sort the hand, and end the turn without immediately playing it.
  5. Return the first player whose hand becomes empty. If the draw pile is empty and a full round passes with no play, return -1.

Function

matchingCardGameWinner(deckOrder: String[], handSize: int) → int

Examples

Example 1

deckOrder = ["1-A","2-B","3-C","4-D","5-A"]handSize = 1return = 0

Player 0 plays 1-A on 5-A and empties their hand.

Example 2

deckOrder = ["1-A","2-B","3-C","4-D","9-B","8-A"]handSize = 1return = 1

Player 0 draws 8-A and ends the turn; player 1 then plays 2-B on 9-B.

Example 3

deckOrder = ["1-A","2-B","3-C","4-D","9-E"]handSize = 1return = -1

No hand matches rank 9 or suit E, and no draw cards remain.

Constraints

  • 1 <= handSize and 4*handSize+1 <= deckOrder.length <= 200000.
  • Every card matches [1-9][0-9]*-[A-Z]+.
  • The supplied deck order is deterministic; duplicate card codes are allowed.

More Scale AI problems

See Scale AI hiring insights
public int matchingCardGameWinner(String[] deckOrder, int handSize) {
    // write your code here
}
deckOrder["1-A","2-B","3-C","4-D","5-A"]
handSize1
expected0
Checking account…