Two-Player Gambler's Ruin Probability and Simulation
Updated
Problem statement
Two players play independent games: one player's results never affect the other's. Each player starts with wealth 1. On each play, a player gains one with probability winProbability and loses one otherwise. A player's game ends when their wealth reaches 0 or target.
Return two values. The first is the exact probability that both players reach zero before reaching target. The second comes from a simulation: the fraction of paired trials in which both players reach zero.
The simulated games are given to you. Row i of trialsA and row i of trialsB together make one paired trial. Each row lists that player's steps in order, with 1 for a win and -1 for a loss. Every row is guaranteed to end exactly when the player's wealth hits zero or target.
Function
gamblersRuin(target: int, winProbability: double, trialsA: int[][], trialsB: int[][]) → double[]Examples
Example 1
target = 3winProbability = 0.5trialsA = [[-1],[1,-1,-1],[1,1]]trialsB = [[-1],[-1],[1,-1,-1]]return = [0.4444444444444444,0.6666666666666666]Here the game is fair: each play is a win half the time. A player starts at wealth one and stops at zero or three. In a fair game, one player reaches zero with probability 2/3. The two games are independent, so both players reach zero with probability 4/9.
Now look at the given trials pair by pair. In the first pair, both players lose right away. In the second pair, both players end at zero. In the third pair, the trialsA player wins twice and reaches three. So two of the three pairs end with both players at zero.
Example 2
target = 2winProbability = 0.5trialsA = [[-1],[1]]trialsB = [[-1],[-1]]return = [0.25,0.5]With target two and a fair game, each player reaches zero with probability one half. Both do so with probability one quarter. In the first pair, both players lose right away. In the second pair, the trialsA player wins and stops at two. So one of the two pairs ends with both players at zero.
Constraints
2 <= target <= 1000 < winProbability < 11 <= trialsA.length = trialsB.length <= 1000- Every trial row contains only
-1and1and reaches zero or target exactly - Each returned value is accepted if it is within absolute error
1e-9of the expected value
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Robot Survival ProbabilityInMobi · ONSITE INTERVIEWInMobi interview questions
- Knockout Tournament Win ProbabilityGoogle · PHONE SCREENGoogle interview questions
- Allocate Mailboxes with Squared DistanceLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Grouped-Query Attention with a KV CacheAmazon · ONSITE INTERVIEW
- Maximum Bananas Delivered by a CamelStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Pro-Rata Order BookIMC Trading · ONSITE INTERVIEWIMC Trading interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Count Ideal ArraysMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions