Two Ranked Teams Meeting in a Random Bracket
Updated
Problem statement
A knockout tournament has teams teams, ranked from 1 (strongest) to teams (weakest). When two teams play, the team with the lower rank number wins and stays in; the other team is out.
The bracket has teams lines in a fixed order. The opening round pairs line 1 with line 2, line 3 with line 4, and so forth up to the last two lines. Each later round pairs the winners of neighbouring blocks: the team that comes out of lines 1-2 meets the team that comes out of lines 3-4, the team from lines 1-4 meets the team from lines 5-8, and this goes on until one team is left.
Before the tournament starts, the teams are put on the lines at random: every one of the teams! orders is equally likely. There is no seeding that keeps the strong teams apart.
A round is named by teamsLeft, the number of teams still in the tournament when that round starts. So teamsLeft = teams is the opening round, teamsLeft = 4 is the semifinal and teamsLeft = 2 is the final.
Find the probability that the teams ranked rankA and rankB play each other in the round named by teamsLeft, and return it.
Function
meetProbability(teams: int, rankA: int, rankB: int, teamsLeft: int) → doubleExamples
Example 1
teams = 64rankA = 2rankB = 3teamsLeft = 4return = 0.13108038914490527Put rank 2 on any line. The semifinal pairs two blocks of 16 lines, so rank 3 has to land in the other block of 16 inside the same half: 16 of the 63 open lines. Rank 1 must then land outside that half, or it would knock one of them out first: 32 of the 62 lines left. The chance is (16/63) · (32/62) = 256/1953, about 0.131080.
Example 2
teams = 8rankA = 2rankB = 3teamsLeft = 2return = 0.0A final between ranks 2 and 3 needs each of them to win a half. Rank 1 sits in one of those halves and beats whoever is there, so this never happens: the answer is 0.
Example 3
teams = 8rankA = 3rankB = 5teamsLeft = 4return = 0.08571428571428572The round with 4 teams left pairs two blocks of 2 lines. Rank 5 lands in the block next to rank 3 with chance 2/7. Ranks 1 and 2 must both sit on the 4 lines outside those blocks: 4/6 · 3/5. Rank 4 may share rank 3's block (rank 3 beats it) but not rank 5's: 3 of the 4 lines left. Altogether (2/7) · (4/6) · (3/5) · (3/4) = 3/35, about 0.085714.
Constraints
teams = 2^kfor a whole numberkwith1 <= k <= 20, so2 <= teams <= 1048576.1 <= rankA < rankB <= teamsteamsLeft = 2^jfor a whole numberjwith1 <= j <= k, so2 <= teamsLeft <= teams.- Your answer is accepted when it differs from the correct probability by at most
10^-6.
More Optiver Online Assessment questions
- Chance a Die Game Balance Reaches ZeroOA · Seen Dec 2026
- Chance a Hand of Cards Multiplies to More Than a ThresholdOA · Seen Dec 2026
- Chance a Sum of Random Draws Has a Given Number of DigitsOA · Seen Dec 2026
- Expected Draws to Remove All Green CountersOA · Seen Dec 2026
- Expected Profit of a Doubling Betting SystemOA · Seen Dec 2026
- Expected Steps Until a Random Walk Gets Far From ZeroOA · Seen Dec 2026
- Rolling Until One Face's Own Total Passes a ThresholdOA · Seen Dec 2026
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
Similar questions at other companies
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Get Triplet CountIBM · OAIBM interview questions
- Count Fancy NumbersHudson River Trading · OAHudson River Trading interview questions
- Count Numbers with the Same Set-Bit CountMicrosoft · OAMicrosoft interview questions
- Count Reverse-Digit PairsVisa · OAVisa interview questions
- Get Triplet CountPoint72 · OAPoint72 interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Color a Grid with No Monochromatic Row or ColumnIntuit · OAIntuit interview questions
- Product of Subset MaximaArcesium · OAArcesium interview questions