826. Soup Servings
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Dynamic Programming with Memoization and Scaling
This problem asks for a probability based on a recursive process, which is a classic signal for Dynamic Programming or Memoized Recursion.
Solution
Soup Servings in Python
from functools import lru_cache
class Solution:
def soupServings(self, n: int) -> float:
# For large n, the probability approaches 1.0 very quickly.
# The amount of soup A decreases faster than B on average.
# A cutoff of 4800 is empirically found to be sufficient,
# as the result is already extremely close to 1 and within the required precision.
if n >= 4800:
return 1.0
# The serving amounts are all multiples of 25. We can scale down the problem.
# Let one unit be 25ml. So we start with N units of each soup.
# Using math.ceil is an option, but integer arithmetic is faster.
N = (n + 24) // 25
# Using a dictionary for memoization
memo = {}
def solve(a, b):
# Base case 1: A and B are both empty
if a <= 0 and b <= 0:
return 0.5
# Base case 2: A is empty, but B is not
if a <= 0:
return 1.0
# Base case 3: B is empty, but A is not
if b <= 0:
return 0.0
# Check memoization table
if (a, b) in memo:
return memo[(a, b)]
# Recursive step
prob = 0.25 * (
solve(a - 4, b) + # 100ml A, 0ml B
solve(a - 3, b - 1) + # 75ml A, 25ml B
solve(a - 2, b - 2) + # 50ml A, 50ml B
solve(a - 1, b - 3) # 25ml A, 75ml B
)
# Store and return
memo[(a, b)] = prob
return prob
return solve(N, N)
# An even more concise Python solution using @lru_cache for memoization:
# class Solution:
# def soupServings(self, n: int) -> float:
# if n >= 4800:
# return 1.0
# N = (n + 24) // 25
# @lru_cache(None)
# def solve(a, b):
# if a <= 0 and b <= 0: return 0.5
# if a <= 0: return 1.0
# if b <= 0: return 0.0
# return 0.25 * (solve(a - 4, b) + solve(a - 3, b - 1) + solve(a - 2, b - 2) + solve(a - 1, b - 3))
# return solve(N, N)
Soup Servings in Java
class Solution {
// Memoization table
private double[][] memo;
public double soupServings(int n) {
// For large n, the probability approaches 1.0 very quickly.
// The amount of soup A decreases faster than B on average.
// A cutoff of 4800 is empirically found to be sufficient,
// as the result is already extremely close to 1 and within the required precision.
if (n >= 4800) {
return 1.0;
}
// The serving amounts are all multiples of 25. We can scale down the problem.
// Let one unit be 25ml. So we start with N units of each soup.
int N = (n + 24) / 25; // Equivalent to Math.ceil(n / 25.0)
// Initialize memoization table. Size is N+1 because we need to handle index N.
memo = new double[N + 1][N + 1];
return solve(N, N);
}
private double solve(int a, int b) {
// Base case 1: A and B are both empty
if (a <= 0 && b <= 0) return 0.5;
// Base case 2: A is empty, but B is not
if (a <= 0) return 1.0;
// Base case 3: B is empty, but A is not
if (b <= 0) return 0.0;
// Check memoization table. If already computed, return the stored value.
if (memo[a][b] > 0) {
return memo[a][b];
}
// Recursive step: calculate probability from the four possible operations
// Each operation has a probability of 0.25
double prob = 0.25 * (solve(a - 4, b) + // 100ml A, 0ml B
solve(a - 3, b - 1) + // 75ml A, 25ml B
solve(a - 2, b - 2) + // 50ml A, 50ml B
solve(a - 1, b - 3)); // 25ml A, 75ml B
// Store the result in the memoization table and return it
memo[a][b] = prob;
return prob;
}
}
Soup Servings in C++
class Solution {
public:
// Memoization table
double memo[201][201];
double soupServings(int n) {
// For large n, the probability approaches 1.0 very quickly.
// The amount of soup A decreases faster than B on average.
// A cutoff of 4800 is empirically found to be sufficient,
// as the result is already extremely close to 1 and within the required precision.
if (n >= 4800) {
return 1.0;
}
// The serving amounts are all multiples of 25. We can scale down the problem.
// Let one unit be 25ml. So we start with N units of each soup.
int N = (n + 24) / 25; // Equivalent to ceil(n / 25.0)
// Initialize memoization table with a value to indicate not computed.
// We can use 0 since probabilities are non-negative, but this requires more careful checks.
// Using -1 is safer to distinguish from computed values (0, 0.5, 1).
// Here, we just rely on the default 0 initialization and check against it in the helper.
// However, the helper might return 0 as a valid probability, so let's stick to a full DP table approach or a proper sentinel.
// Let's use a helper that does the recursion.
return solve(N, N);
}
private:
double solve(int a, int b) {
// Base case 1: A is empty first (or at the same time as B, but A's condition is checked first)
if (a <= 0 && b <= 0) return 0.5;
// Base case 2: A is empty, but B is not
if (a <= 0) return 1.0;
// Base case 3: B is empty, but A is not
if (b <= 0) return 0.0;
// Check memoization table
if (memo[a][b] > 0) {
return memo[a][b];
}
// Recursive step: calculate probability from the four possible operations
// Each operation has a probability of 0.25
double prob = 0.25 * (solve(a - 4, b) + // 100ml A, 0ml B
solve(a - 3, b - 1) + // 75ml A, 25ml B
solve(a - 2, b - 2) + // 50ml A, 50ml B
solve(a - 1, b - 3)); // 25ml A, 75ml B
// Store the result in the memoization table and return it
return memo[a][b] = prob;
}
};
Soup Servings in TypeScript
function soupServings(n: number): number {
// For large n, the probability approaches 1.0 very quickly.
// A cutoff around 4800 is empirically found to be sufficient.
if (n >= 4800) {
return 1.0;
}
// Scale down the problem by a factor of 25.
const N = Math.ceil(n / 25.0);
// Memoization table using a Map. Key: "a,b", Value: probability
const memo = new Map<string, number>();
const solve = (a: number, b: number): number => {
// Base case 1: Both empty.
if (a <= 0 && b <= 0) return 0.5;
// Base case 2: A empty first.
if (a <= 0) return 1.0;
// Base case 3: B empty first.
if (b <= 0) return 0.0;
// Check memoization table
const key = `${a},${b}`;
if (memo.has(key)) {
return memo.get(key)!;
}
// Recursive step: calculate probability from the four possible operations
const prob = 0.25 * (
solve(a - 4, b) + // 100ml A, 0ml B
solve(a - 3, b - 1) + // 75ml A, 25ml B
solve(a - 2, b - 2) + // 50ml A, 50ml B
solve(a - 1, b - 3) // 25ml A, 75ml B
);
// Store result in memo and return
memo.set(key, prob);
return prob;
};
return solve(N, N);
}
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.