KatInterview

Expected Wait Time and Coin Combination Counting

Dynamic ProgrammingAuto-graded · 21 test cases
MediumAuto-gradedDynamic Programmingprobabilityexpected-valuecombinatorics

Last 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 position k where switching is better (integer constant: 26)
  • "coin_combinations": number of distinct ways to make n cents (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.