Soup Servings - Problem
Soup Servings is a fascinating probability problem that simulates a random serving process.
You have two soups, A and B, each starting with
๐ฅ 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.
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
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
โ Quadratic Growth
Space Complexity
O(nยฒ)
Memoization table stores up to nยฒ/625 states plus recursion stack
โ Quadratic Space
Constraints
- 0 โค n โค 109
- Answers within 10-5 of the actual answer will be accepted
- All operations pour multiples of 25mL
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code