You have two soups, A and B, each starting with n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns:

  • Pour 100 mL from type A and 0 mL from type B
  • Pour 75 mL from type A and 25 mL from type B
  • Pour 50 mL from type A and 50 mL from type B
  • Pour 25 mL from type A and 75 mL from type B

Note: There is no operation that pours 0 mL from A and 100 mL from B. The amounts from A and B are poured simultaneously during the turn. If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup. The process stops immediately after any turn in which one of the soups is used up.

Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn.

Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Small Case
$ Input: n = 50
Output: 0.625
💡 Note: With 50mL each, there are limited serving combinations. A has higher probability of finishing first due to higher average serving rate (62.5 vs 37.5 mL per turn).
Example 2 — Medium Case
$ Input: n = 100
Output: 0.71875
💡 Note: With 100mL each, the probability increases as the law of large numbers makes A's higher serving rate more apparent.
Example 3 — Large Case
$ Input: n = 5000
Output: 1.0
💡 Note: For large n, A's 67% higher serving rate means it will almost certainly finish first. The probability approaches 1.0.

Constraints

  • 0 ≤ n ≤ 109

Visualization

Tap to expand
Soup Servings: Probability AnalysisSoup A200mLSoup B200mLFour Random Operations (25% each)1. Serve 100mL from A, 0mL from B2. Serve 75mL from A, 25mL from B3. Serve 50mL from A, 50mL from B4. Serve 25mL from A, 75mL from BKey ObservationAverage A serving: (100+75+50+25)/4 = 62.5 mL per turnAverage B serving: (0+25+50+75)/4 = 37.5 mL per turnA finishes 67% faster → Higher probability!Return: P(A first) + 0.5 × P(both together)
Understanding the Visualization
1
Initial State
Both soups start with n mL
2
Random Operations
Four operations with different A:B serving ratios
3
Probability
Calculate P(A finishes first) + 0.5×P(both finish together)
Key Takeaway
🎯 Key Insight: Soup A has a higher average serving rate, making it more likely to finish first
Asked in
Google 15 Microsoft 12
12.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen