Expected Wait Time and Coin Combination Counting
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Tests probability reasoning with independent random events and dynamic programming for combinatorial counting with unlimited resources.
This problem has two independent parts that must be solved together.
Part 1: Checkout Lane Decision
A store has 100 customers in a single checkout line, numbered from position 1 (front, next to be served) to position 100 (back). Each customer's service takes exactly one time unit. You are currently at position k.
A second checkout lane opens immediately. At this moment, each of the other 99 customers independently decides whether to move to the new lane, each with probability 1/2. If customers move to the new lane (possibly including you), they are arranged in a uniformly random order—every permutation of the movers is equally likely. Both lanes then operate simultaneously, each serving one customer per time unit.
Important: Waiting time is measured as the number of time units until your service begins (equivalently, the number of customers served before you). A customer at position 1 waits 0 time units.
Your task for Part 1:
Calculate the expected waiting time if you stay in the original lane, the expected waiting time if you switch to the new lane, and determine the threshold position where switching becomes preferable.
Part 2: US Coin Combinations
You have an unlimited supply of US coins with the following denominations (in cents):
- 1 cent (penny)
- 5 cents (nickel)
- 10 cents (dime)
- 25 cents (quarter)
- 100 cents (dollar coin)
Given a target amount n cents, count how many distinct ways you can form exactly n cents using these coins. Two ways are considered the same if they use the same quantity of each denomination, regardless of order. For example, one nickel plus one penny is the same combination as one penny plus one nickel.
Your task for Part 2:
Return the number of distinct coin combinations that sum to exactly n cents.
Function Specification
Implement a function that returns a dictionary containing:
"stay_wait": expected waiting time if you stay (integer)"switch_wait": expected waiting time if you switch (float)"switch_better": boolean indicating whether switching gives lower expected wait time"switch_threshold": the smallest positionkwhere switching is better (integer constant: 26)"coin_combinations": number of distinct ways to makencents (integer)
Constraints
- 1 ≤ k ≤ 100
- 0 ≤ n ≤ 10,000
- The original queue has exactly 100 customers total
- Each of the other 99 customers moves independently with probability exactly 1/2
- If you stay, customers who leave do not affect your position (your wait remains k-1)
Examples
Example 1
Input: compute_wait_and_combinations(1, 0)
Output: {'stay_wait': 0, 'switch_wait': 24.75, 'switch_better': False, 'switch_threshold': 26, 'coin_combinations': 1}
Example 2
Input: compute_wait_and_combinations(25, 11)
Output: {'stay_wait': 24, 'switch_wait': 24.75, 'switch_better': False, 'switch_threshold': 26, 'coin_combinations': 4}
Example 3
Input: compute_wait_and_combinations(26, 100)
Output: {'stay_wait': 25, 'switch_wait': 24.75, 'switch_better': True, 'switch_threshold': 26, 'coin_combinations': 243}
Example 4
Input: compute_wait_and_combinations(100, 10000)
Output: {'stay_wait': 99, 'switch_wait': 24.75, 'switch_better': True, 'switch_threshold': 26, 'coin_combinations': 3430874151}
Hints
Hint 1
For Part 1: Use linearity of expectation. The expected number of other customers who move follows a binomial distribution with n=99 and p=0.5.
Hint 2
For Part 1: If you switch, your position among all movers is uniformly random. What is the expected number of people ahead of you, given M movers total?
Hint 3
For Part 2: This is the unbounded knapsack problem for counting combinations. Process coins in a fixed order (outer loop) and update amounts in ascending order (inner loop) to avoid counting permutations.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1: Expected Waiting Time Analysis
If you stay: Your position remains fixed at k, so you wait for exactly k - 1 customers ahead of you to be served. This is deterministic: E[stay] = k - 1.
If you switch: Model the number of other customers who move as a random variable M. Each of the 99 other customers independently moves with probability 1/2, so M follows a binomial distribution with expected value E[M] = 99 × 0.5 = 49.5.
If you move to the new lane, it contains M + 1 people total (the M other movers plus you), arranged in uniformly random order. Your position among these movers is equally likely to be anywhere from 1 to M + 1. The number of customers ahead of you is therefore uniform on {0, 1, ..., M}, with conditional expectation M/2.
By the law of total expectation:
E[switch] = E[M/2] = E[M]/2 = 49.5/2 = 24.75
Threshold: Switching is better when k - 1 > 24.75, which simplifies to k > 25.75. Since k is an integer, switching is preferable when k ≥ 26.
Part 2: Coin Combination Counting
This is the classic unbounded knapsack counting problem. We want to count multisets (combinations where order doesn't matter) of coins that sum to n.
Key insight: Process coins in a fixed order to avoid counting the same multiset multiple times. Use a 1D dynamic programming array where dp[amount] represents the number of ways to form that amount using only the coin denominations processed so far.
Algorithm:
- Initialize
dp[0] = 1(one way to make zero: use no coins) and all other entries to 0 - For each coin denomination in a fixed order (1, 5, 10, 25, 100):
- For each amount from
cointonin ascending order:- Update
dp[amount] += dp[amount - coin]
- Update
- For each amount from
Why this works:
- Outer loop = coins: Processing coins in the outer loop ensures we build combinations in canonical order (all pennies first, then nickels, etc.), preventing double-counting of
{1,5}and{5,1}. - Inner loop ascending: When we process amount
v,dp[v - coin]may already include uses of the current coin. This allows unlimited copies. (A descending loop would give the 0/1 knapsack variant.)
Complexity
Part 1:
- Time: O(1) — just arithmetic operations
- Space: O(1)
Part 2:
- Time: O(D × n) where D = 5 denominations, so O(5n) = O(n)
- Space: O(n) for the dp array
For n = 10,000, this requires about 50,000 operations, which is very efficient.
Complete Solution
from typing import Dict
def compute_wait_and_combinations(k: int, n: int) -> Dict[str, object]:
# Part 1: Queue switching analysis
# If you stay at position k, you wait for the k-1 customers ahead of you
stay_wait = k - 1
# If you switch: the other 99 customers each move with probability 1/2,
# so the expected number of other movers is 99/2. Among all movers
# (including you), your position is uniformly random, so the expected
# number of customers ahead of you is half the other movers: 99/4.
switch_wait = 99 / 4
switch_better = stay_wait > switch_wait
# Part 2: Unbounded coin change counting
# dp[amount] = number of ways to make that amount using coins processed so far
dp = [0] * (n + 1)
dp[0] = 1 # One way to make 0: use no coins
# Process each denomination in fixed order (combinations, not permutations)
for coin in (1, 5, 10, 25, 100):
# Ascending order allows unlimited copies of each coin
for amount in range(coin, n + 1):
dp[amount] += dp[amount - coin]
return {
"stay_wait": stay_wait,
"switch_wait": switch_wait,
"switch_better": switch_better,
"switch_threshold": 26,
"coin_combinations": dp[n],
}