Compare Complete or Partial Hands
RipplingSoftware Engineer · OnsiteLast updated
This question evaluates implementation of card-hand evaluation logic, combinatorial reasoning about incomplete information, and deterministic tie-breaking rules for comparing complete and partial poker-like hands.
Implement a hand comparison engine for a simplified poker-like card game.
Each player has a hand represented by an array of card strings. A card is encoded as <rank><suit>, where rank is one of 2-10, J, Q, K, A, and suit is one of S, H, D, C.
Part 1: Compare complete hands
If both players have exactly 5 cards, return one of:
"user1"if player 1 wins"user2"if player 2 wins"tie"if the hands are exactly equal in strength
Use the following hand rankings from strongest to weakest:
- Four of a kind
- Full house
- Three of a kind
- Two pair
- One pair
- High card
Tie-breaking rules:
- Four of a kind: compare the rank of the four matching cards, then the kicker.
- Full house: compare the rank of the triple, then the pair.
- Three of a kind: compare the rank of the triple, then the remaining two cards in descending order.
- Two pair: compare the higher pair, then the lower pair, then the kicker.
- One pair: compare the pair rank, then the remaining three cards in descending order.
- High card: compare all five ranks in descending order.
Assume suits do not affect hand strength.
Part 2: Compare partial hands
Because of network delay, a player may have fewer than 5 received cards. In this case, determine whether the result is already forced.
Return:
"user1"if player 1 wins for every valid completion of the missing cards"user2"if player 2 wins for every valid completion of the missing cards"tie"if every valid completion ends in a tie"unknown"otherwise
For this follow-up, assume:
- Each final hand must contain exactly 5 distinct cards.
- Missing cards can be any distinct cards from the remaining standard 52-card deck.
- No card may appear twice across the final 10 cards.
Design the comparison logic cleanly so that ranking rules are modular and easy to extend.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.