FastPrepTwo-Player Gambler's Ruin Probability and Simulation
Problems/Two-Player Gambler's Ruin Probability and Simulation

Two-Player Gambler's Ruin Probability and Simulation

Microsoft logoMicrosoft● HardNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft Data Scientist internship onsite asks for the probability that two wealth-one gamblers both reach zero and then asks for a simulation. Independent games, a shared upper target, win probability input, boundary stopping, supplied realized trials, return shape, and bounds are conventional enrichment.

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 <= 100
  • 0 < winProbability < 1
  • 1 <= trialsA.length = trialsB.length <= 1000
  • Every trial row contains only -1 and 1 and reaches zero or target exactly
  • Each returned value is accepted if it is within absolute error 1e-9 of 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

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double[] gamblersRuin(int target, double winProbability, int[][] trialsA, int[][] trialsB) {
      // Write your code here.
    }

}
Checking account…