Expected Pairs in a Dealt Hand
Updated
Problem statement
A deck has ranks different ranks and exactly two cards of each rank, so it holds 2 * ranks cards. The deck is shuffled well, and you are dealt handSize cards without replacement. Every set of handSize cards is equally likely to be your hand.
A pair is a rank whose two cards are both in your hand. Return the expected number of pairs in your hand.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedPairs(ranks: int, handSize: int) → doubleExamples
Example 1
ranks = 4handSize = 5return = 1.4285714285714286This is the interview case: an 8-card deck with two cards each of 4 ranks, and a 5-card hand. There are C(8, 5) = 56 equally likely hands. Five cards from only four ranks always include at least one pair.
A hand has exactly one pair in 4 × 8 = 32 ways: choose the paired rank, then one of the two cards of each other rank (2^3 = 8 ways). It has two pairs in 6 × 4 = 24 ways: choose the two paired ranks (6 ways), then one of the 4 remaining cards. The answer is (32 × 1 + 24 × 2) / 56 = 80/56 = 10/7, about 1.4286.
Example 2
ranks = 2handSize = 2return = 0.3333333333333333The deck has 4 cards: two of the first rank and two of the second. There are 6 possible two-card hands, and 2 of them hold both cards of one rank. The answer is 2/6 = 1/3.
Example 3
ranks = 3handSize = 4return = 1.2The deck has 6 cards, and the hand leaves out 2 of them. There are C(6, 2) = 15 ways to pick the two left-out cards. In 3 of them both left-out cards have the same rank, so the hand holds the other 2 ranks as pairs. In the other 12 they have different ranks, so the hand has only 1 pair. The answer is (3 × 2 + 12 × 1) / 15 = 18/15 = 1.2.
Constraints
1 <= ranks <= 10^61 <= handSize <= 2 * ranks
More Susquehanna International Group (SIG) Online Assessment questions
- Catch Fish with Reusable BaitsOA · Seen Nov 2026
- Bold Play to Reach a GoalOA · Seen Nov 2026
- Cats, Truth and Who Has MoreOA · Seen Nov 2026
- Counting Cows by Their LegsOA · Seen Nov 2026
- Enough Muffins for the LineOA · Seen Nov 2026
- Expected Consecutive Pairs Among Chosen NumbersOA · Seen Nov 2026
- Four-by-Four Boundary GridOA · Seen Nov 2026
- Frog Paths Without Long RunsOA · Seen Nov 2026
- Higher Roll, or Lose the SumOA · Seen Nov 2026
- Is a Popular Story Fake?OA · Seen Nov 2026
Similar questions at other companies
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Subarray SumVisa · OAVisa interview questions
- Drawing EdgeWeRide · OAWeRide interview questions
- Drawing EdgeIBM · OAIBM interview questions
- Drawing EdgeSnowflake · OASnowflake interview questions
- Process SchedulingCitadel · OACitadel interview questions
- Unique Digits in RangeAgoda · OAAgoda interview questions
- Ordered Book Displays with a Featured BookIMC Trading · OAIMC Trading interview questions
- Find Max Number of PairsRoblox · OARoblox interview questions
- Count Nondecreasing Digit-Sum ArraysMicrosoft · OAMicrosoft interview questions