Two-Player Three-Card Poker Simulation: Deal Hands and Decide the Winner

Quick Overview

A coding exercise to build a two-player poker simulation: deal three cards to each player from a shuffled deck, classify each hand as a straight flush, three of a kind, straight, flush, pair or high card, and pick the winner. It tests card modeling, ranking and tie-break logic, and testing.

Two-Player Three-Card Poker Simulation: Deal Hands and Decide the Winner

Company: OpenAI

Role: Machine Learning Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

Write a small poker simulation for exactly two players. Deal each player three cards from a shuffled standard 52-card deck, work out which kind of hand each player holds, and decide who wins. Keep the program to two players and three cards each: generalizing to other numbers of players or cards is not needed, and comparing the hand types directly, case by case, is the expected approach. The hand ranking was not reported. Unless the interviewer gives a different one, assume the standard three-card ranking below, from strongest to weakest. Cards are written as a rank (`2` to `9`, `T`, `J`, `Q`, `K`, `A`) followed by a suit (`c`, `d`, `h`, `s`). | Order | Hand | Definition | Example | | --- | --- | --- | --- | | 1 | Straight flush | Three consecutive ranks in one suit | `5h 6h 7h` | | 2 | Three of a kind | Three cards of the same rank | `Qc Qd Qs` | | 3 | Straight | Three consecutive ranks, not all in one suit | `9c Td Jh` | | 4 | Flush | Three cards in one suit whose ranks are not consecutive | `2s 8s Ks` | | 5 | Pair | Exactly two cards of the same rank | `4d 4h Ac` | | 6 | High card | None of the above | `3c 9d Kh` | With three cards a straight is rarer than a flush, so it ranks higher: the reverse of the familiar five-card order. ```hint Reduce a hand to one comparable value Hands in different categories are decided by category alone; hands in the same category need a tie-break. Decide what single value captures both, so that picking the winner becomes one comparison. ``` ```hint Check the boundary hands With only three cards you can list every category explicitly. Before you finish, test the hands that sit on a boundary: an ace-low straight, two pairs of the same rank, and two hands that are exactly equal. ``` ### Constraints and Clarifications - Exactly two players and three cards each, dealt without replacement from one 52-card deck with no jokers, so the six cards dealt are distinct. - Assumed tie-breaks, to confirm with the interviewer: within a category, compare ranks from the highest down; a pair is compared on the rank of the pair first, then on the remaining card; the ace plays high in Q-K-A and low in A-2-3, which is the lowest straight, and K-A-2 is not a straight; suits never break a tie, so two hands with the same ranks in the same category tie. - Not reported: how the program receives input and reports the result, and whether it plays one hand or many. Ask, or state your assumptions. ### Clarifying Questions - Which ranking applies: the three-card order above, in which a straight beats a flush, or the five-card order? - Can the ace play low, and does K-A-2 count as a straight? - When both hands are equal, is the result a tie, or do suits break it? - Should the program deal and play one round, compare two hands it is given, or play many rounds and report how often each player wins? - Must the dealing be reproducible, for example from a seed, so that a run can be replayed in a test? ### What a Strong Answer Covers - Settles the ranking order, the ace rule and the tie rule before coding instead of assuming the five-card order - A clear model of cards and the deck, and dealing without replacement from a seedable random source - A hand evaluator that maps every hand to its category and the ranks that break ties, including the ace-low straight - A winner decision that reports exact ties, with code kept to two players and three cards as asked - Tests that give confidence every possible three-card hand is classified correctly ### Follow-up Questions - Player A has been dealt a specific hand. How would you compute the probability that A wins, exactly or by simulation, and which would you choose? - How would you extend the program to any number of players, and what would have to change for five-card hands? - How would you convince yourself that the evaluator classifies every possible three-card hand correctly? - A simulation of many millions of rounds is too slow. Where does the time go, and how would you speed it up?

Overview: A coding exercise to build a two-player poker simulation: deal three cards to each player from a shuffled deck, classify each hand as a straight flush, three of a kind, straight, flush, pair or high card, and pick the winner. It tests card modeling, ranking and tie-break logic, and testing.

OpenAI logo
OpenAI
Oct 25, 2026
hardMachine Learning EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

Write a small poker simulation for exactly two players. Deal each player three cards from a shuffled standard 52-card deck, work out which kind of hand each player holds, and decide who wins. Keep the program to two players and three cards each: generalizing to other numbers of players or cards is not needed, and comparing the hand types directly, case by case, is the expected approach.

The hand ranking was not reported. Unless the interviewer gives a different one, assume the standard three-card ranking below, from strongest to weakest. Cards are written as a rank (2 to 9, T, J, Q, K, A) followed by a suit (c, d, h, s).

OrderHandDefinitionExample
1Straight flushThree consecutive ranks in one suit5h 6h 7h
2Three of a kindThree cards of the same rankQc Qd Qs
3StraightThree consecutive ranks, not all in one suit9c Td Jh
4FlushThree cards in one suit whose ranks are not consecutive2s 8s Ks
5PairExactly two cards of the same rank4d 4h Ac
6High cardNone of the above3c 9d Kh

With three cards a straight is rarer than a flush, so it ranks higher: the reverse of the familiar five-card order.

Constraints and Clarifications

  • Exactly two players and three cards each, dealt without replacement from one 52-card deck with no jokers, so the six cards dealt are distinct.
  • Assumed tie-breaks, to confirm with the interviewer: within a category, compare ranks from the highest down; a pair is compared on the rank of the pair first, then on the remaining card; the ace plays high in Q-K-A and low in A-2-3, which is the lowest straight, and K-A-2 is not a straight; suits never break a tie, so two hands with the same ranks in the same category tie.
  • Not reported: how the program receives input and reports the result, and whether it plays one hand or many. Ask, or state your assumptions.

Clarifying Questions Guidance

  • Which ranking applies: the three-card order above, in which a straight beats a flush, or the five-card order?
  • Can the ace play low, and does K-A-2 count as a straight?
  • When both hands are equal, is the result a tie, or do suits break it?
  • Should the program deal and play one round, compare two hands it is given, or play many rounds and report how often each player wins?
  • Must the dealing be reproducible, for example from a seed, so that a run can be replayed in a test?

What a Strong Answer Covers Guidance

  • Settles the ranking order, the ace rule and the tie rule before coding instead of assuming the five-card order
  • A clear model of cards and the deck, and dealing without replacement from a seedable random source
  • A hand evaluator that maps every hand to its category and the ranks that break ties, including the ace-low straight
  • A winner decision that reports exact ties, with code kept to two players and three cards as asked
  • Tests that give confidence every possible three-card hand is classified correctly

Follow-up Questions Guidance

  • Player A has been dealt a specific hand. How would you compute the probability that A wins, exactly or by simulation, and which would you choose?
  • How would you extend the program to any number of players, and what would have to change for five-card hands?
  • How would you convince yourself that the evaluator classifies every possible three-card hand correctly?
  • A simulation of many millions of rounds is too slow. Where does the time go, and how would you speed it up?