Compare Complete or Partial Hands
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
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:
1. Four of a kind
2. Full house
3. Three of a kind
4. Two pair
5. One pair
6. 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.
Quick Answer: 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.
Part 1: Compare Complete Hands
Implement a hand comparison engine for a simplified poker-like card game.
Each player has exactly 5 cards. 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.
Suits do not affect hand strength. Only the following hand types exist, from strongest to weakest:
1. Four of a kind
2. Full house
3. Three of a kind
4. Two pair
5. One pair
6. 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 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.
Return 'user1' if player 1 wins, 'user2' if player 2 wins, or 'tie' if the hands are exactly equal in strength.