Expected Steps to Deplete Whole Pills from a Random Draw Process
Dynamic ProgrammingAuto-graded · 14 test casesLast updated
Tests modeling a stochastic process as a Markov chain and solving it with dynamic programming using conditional expectation.
You begin with a container holding n complete pills. Each step, you randomly select one pill from the container with uniform probability:
- If you select a complete pill, you consume half of it and return the remaining half to the container.
- If you select a half pill, you consume it entirely and nothing is returned.
Your task is to compute the expected number of steps required until no complete pills remain in the container.
Constraints
1 <= n <= 1000- Return the result as a floating-point number.
- The process terminates when the count of complete pills reaches zero (half pills may still remain).
Examples
Example 1
Input: steps_until_no_complete(1)
Output: 1
Example 2
Input: steps_until_no_complete(2)
Output: 2.5
Hints
Hint 1
Model the system state as a pair (complete_count, half_count) and write a recurrence for the expected steps from each state.
Hint 2
The base case is when complete_count = 0, which requires zero additional steps.
Hint 3
Use dynamic programming with states ordered by increasing complete_count, then increasing half_count within each row.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We model this as a Markov chain where the state is represented by (w, h): the count of complete pills and half pills currently in the container.
Define E(w, h) as the expected number of steps needed from state (w, h) until w reaches zero.
Base case: When w = 0, no complete pills remain, so E(0, h) = 0 for any h.
Recursive case: When w >= 1, we take one step and transition based on what we draw:
- With probability
w / (w + h), we draw a complete pill and move to state(w-1, h+1) - With probability
h / (w + h), we draw a half pill and move to state(w, h-1)
This gives the recurrence:
E(w, h) = 1 + (w/(w+h)) * E(w-1, h+1) + (h/(w+h)) * E(w, h-1)
State space bound: Starting from (n, 0), each complete pill can create at most one half pill. Therefore, h <= n - w, which means w + h <= n. This bounds the state space to roughly n²/2 states.
Evaluation order: To compute E(w, h), we need E(w-1, h+1) from the previous row and E(w, h-1) from the current row. We process rows in increasing order of w, and within each row process increasing h. This ensures all dependencies are available when needed.
We use a rolling array optimization, keeping only two rows in memory at once.
Correctness
The Markov chain always terminates because each transition either decreases w or keeps w fixed while decreasing h. The recurrence follows from the law of total expectation: we spend one step, then add the expected remaining time from whichever next state we reach.
Complexity
- Time: O(n²) — we compute approximately
n²/2states - Space: O(n) — using rolling arrays to store only two rows at a time
Solution
def steps_until_no_complete(n: int) -> float:
"""
Compute the expected number of steps until no complete pills remain,
starting with n complete pills and 0 half pills.
:param n: Initial number of complete pills
:return: Expected number of steps
"""
if n <= 0:
return 0.0
# prev[h] stores E(w-1, h) for the previous row
prev = [0.0] * (n + 1)
for w in range(1, n + 1):
max_h = n - w
cur = [0.0] * (n + 1)
for h in range(max_h + 1):
total = w + h
val = 1.0 + (w / total) * prev[h + 1]
if h > 0:
val += (h / total) * cur[h - 1]
cur[h] = val
prev = cur
return prev[0]