Expected Profit of a Doubling Betting System
Updated
Problem statement
You start with bankroll dollars and play a betting game. Every bet succeeds with probability p, independent of every other bet. Bets pay even money: when a bet of s dollars succeeds you gain s dollars, and when it fails you give up those s dollars.
You follow a doubling system. Your first bet is 1 dollar. After a loss, your next bet is twice the bet you just lost. After a win, your next bet goes back to 1 dollar.
The game ends once you have won wins bets in total. It also ends when your next bet would be more than the money you have left: you never bet money you do not have, and you never bet less than the system says. Any money left at that point stays with you.
Your profit is the money you hold when you stop minus bankroll. Return the expected value of your profit. An answer counts as correct if it is off by at most 10^-9 when the correct value is from -1 to 1, and by at most 10^-9 times the size of the correct value otherwise.
Function
expectedProfit(bankroll: long, wins: long, p: double) → doubleExamples
Example 1
bankroll = 31wins = 1p = 0.5return = 0.0The bets 1, 2, 4, 8 and 16 add up to exactly 31. If one of them wins, you end 1 ahead; that happens with chance 31/32. If all five lose (chance 1/32), you are down 31 with nothing left. The expected profit is (31/32) · 1 - (1/32) · 31 = 0.
Example 2
bankroll = 3wins = 2p = 0.6return = 0.6624Each round starts with 3 or 4 dollars, enough for the bets 1 and 2 but not a third bet of 4. A round fails when both bets lose, chance 0.4 · 0.4 = 0.16, and that costs 3. Failing the first round gives -3 (chance 0.16); winning it and failing the second gives 1 - 3 = -2 (chance 0.84 · 0.16 = 0.1344); winning both gives +2 (chance 0.84 · 0.84 = 0.7056). The expected profit is -0.48 - 0.2688 + 1.4112 = 0.6624.
Example 3
bankroll = 2wins = 3p = 0.4return = -0.48864With 2 dollars you can bet 1, but after a loss you cannot cover a bet of 2, so the first round is a single bet. After one win you have 3 dollars, and the rounds after that can afford bets of 1 and 2. Losing the first bet gives -1 (chance 0.6). Winning it and then losing a two-bet round gives 1 - 3 = -2 (chance 0.4 · 0.36 = 0.144). Winning two rounds and losing the third gives 2 - 3 = -1 (chance 0.4 · 0.64 · 0.36 = 0.09216). Winning all three gives +3 (chance 0.4 · 0.64 · 0.64 = 0.16384). The expected profit is -0.48864.
Constraints
1 <= bankroll <= 10^151 <= wins <= 10^150 <= p <= 1- Your answer is accepted when it differs from the correct value by at most
10^-9 · max(1, |correct value|).
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 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
- Two Ranked Teams Meeting in a Random BracketOA · 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
- Bootstrap Decision Tree ClassifierPinterest · OAPinterest interview questions
- Predict Taxi Driver ClassesBoston Consulting Group · OABoston Consulting Group interview questions
- Arithmetic Triplets After QueriesCapital One · OACapital One interview questions
- Bring Servers DownAmazon · OA
- Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Last Object Exit TimeIBM · OAIBM interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Find Total CostJP Morgan Chase · OAJP Morgan Chase interview questions