Soup Servings is a fascinating probability problem that simulates a random serving process.

You have two soups, A and B, each starting with n mL. On every turn, one of four serving operations is chosen randomly with equal probability (0.25 each):

๐Ÿฅ„ Operation 1: Pour 100 mL from A, 0 mL from B
๐Ÿฅ„ Operation 2: Pour 75 mL from A, 25 mL from B
๐Ÿฅ„ Operation 3: Pour 50 mL from A, 50 mL from B
๐Ÿฅ„ Operation 4: Pour 25 mL from A, 75 mL from B

Notice: There's no operation that pours 0 mL from A and 100 mL from B, making soup A more likely to run out first!

If an operation asks to pour more than what remains, pour all that's left. The process stops when at least one soup runs out.

Goal: Return the probability that A runs out before B, plus half the probability that both soups run out simultaneously.

Input & Output

example_1.py โ€” Small Input
$ Input: n = 50
โ€บ Output: 0.62500
๐Ÿ’ก Note: With 50mL each, there are limited possible states. The biased operations make soup A more likely to empty first, resulting in probability โ‰ˆ 0.625
example_2.py โ€” Medium Input
$ Input: n = 100
โ€บ Output: 0.71875
๐Ÿ’ก Note: With 100mL each, the probability increases as the bias toward emptying soup A becomes more pronounced over more operations
example_3.py โ€” Large Input
$ Input: n = 5000
โ€บ Output: 1.00000
๐Ÿ’ก Note: For large values, the mathematical optimization kicks in. The probability approaches 1.0 due to the heavily biased operations

Visualization

Tap to expand
Soup Servings ProbabilitySoup An mLSoup Bn mLFour Random Operations (25% each):Operation 1A: -100mLB: -0mLOperation 2A: -75mLB: -25mLOperation 3A: -50mLB: -50mLOperation 4A: -25mLB: -75mLProbability Formula:P(A first) + 0.5 ร— P(both together)Notice: Operations are biased toward emptying soup A faster!Average A reduction: 62.5mL vs Average B reduction: 37.5mLKey Insight:โ€ข No (0,-100) operationโ€ข A drains faster on averageโ€ข Large n โ†’ probability โ‰ˆ 1.0
Understanding the Visualization
1
Initial State
Both soup pots A and B start with n mL
2
Random Operations
One of 4 operations chosen randomly (notice A-bias)
3
State Transitions
Each operation leads to a new state with reduced amounts
4
Probability Calculation
Compute probability A empties first + 0.5ร—(both empty together)
Key Takeaway
๐ŸŽฏ Key Insight: The operations are biased toward emptying soup A faster (average 62.5mL vs 37.5mL per turn), making the probability approach 1.0 for large inputs!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

At most nยฒ/625 unique states (scaled down by 25), each computed once

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Memoization table stores up to nยฒ/625 states plus recursion stack

n
2n
โš  Quadratic Space

Constraints

  • 0 โ‰ค n โ‰ค 109
  • Answers within 10-5 of the actual answer will be accepted
  • All operations pour multiples of 25mL
Asked in
Google 25 Microsoft 18 Amazon 12 Meta 8
28.5K Views
Medium Frequency
~25 min Avg. Time
845 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