FastPrepDetermine the Winner of Three-Card Hands

Determine the Winner of Three-Card Hands

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row of hands is one player's three-card hand. Cards use rank 2 through 9, T, J, Q, K, or A, followed by suit C, D, H, or S.

For part = 1, rank categories from strongest to weakest as three of a kind, one pair, and high card. For part = 2, insert flush between three of a kind and one pair. Straights are not a category.

Within a category, compare the relevant ranks lexicographically from highest to lowest. A pair compares by pair rank and then kicker. Suits never break a tie. Return the zero-based index of the strongest hand, choosing the smallest index for a complete tie.

Function

winningHand(hands: String[][], part: int) → int

Examples

Example 1

hands = [["7C","7D","2S"],["AH","KD","QC"],["5C","5D","5S"]]part = 1return = 2

Three of a kind outranks both a pair and high card.

Example 2

hands = [["2H","9H","KH"],["AS","AD","3C"]]part = 2return = 0

In part 2, the king-high flush outranks a pair of aces.

Example 3

hands = [["QC","QD","AS"],["QH","QS","KC"],["AC","JD","9S"]]part = 2return = 0

The two pairs share rank queen, so the ace kicker wins.

Constraints

  • 2 <= hands.length <= 100000.
  • Every hand contains exactly three distinct valid cards, and no card appears in two hands.
  • part is 1 or 2.

More OpenAI problems

See OpenAI hiring insights
public int winningHand(String[][] hands, int part) {
    // Write your solution here.
}
hands[["7C","7D","2S"],["AH","KD","QC"],["5C","5D","5S"]]
part1
expected2
Checking account…