Minimum cost to clear a binary array with k-wide windows
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests greedy, sliding window and math.
A packaging line tracks a row of n units, each built as one of two variants: variant A is written 0 and variant B is written 1. You need to convert every unit to variant A.
You are given the binary array product and an integer k (1 <= k <= n). One operation works like this:
- Pick a contiguous block of exactly
kpositions,[l, l + k - 1]. - Pay the current sum of
productover that block, measured before anything changes. - Pick one position in the block that holds a
1and set it to0. The block must contain at least one1.
Return the minimum total cost to make every element 0.
def min_conversion_cost(product: list[int], k: int) -> int: ...
Constraints
1 <= n <= 2 * 10^5-
product[i]is0or1 1 <= k <= n- The answer can exceed 32 bits.
Example 1
product = [1, 1, 1], k = 2
output = 4
Choose block [0, 1] (cost 2) and clear index 1, which gives [1, 0, 1]. Now block [0, 1] costs 1 to clear index 0, and block [1, 2] costs 1 to clear index 2. The total is 4. Clearing index 0 first would cost 2 + 2 + 1 = 5.
Example 2
product = [0, 1, 1, 1, 1, 0], k = 4
output = 7
Every block of length 4 holds at least 3 ones. Clearing the three ones of block [0, 3] while paying for that block each time costs 3 + 2 + 1. The last 1 then costs 1.
Part 2 (if time allows): return one optimal sequence of operations as (l, index_cleared, cost) triples.
Clarifying questions
- Is the cost measured on the block before the chosen
1is cleared, so a block with one1costs1? - If the array is already all zeros, is the answer
0? - Can the same block be chosen again after some of its ones are cleared?
- For
k = n, is the answerm + (m - 1) + ... + 1formones, as a sanity check?
What a strong answer covers
- States the cost of each clear as the number of ones still present in the cheapest block that contains it, so the problem is choosing the order of clears and the block for each.
- Reasons about the process in reverse (start from all zeros and put the ones back), where each one costs the count of ones already back in its best block plus itself; this makes a greedy choice easier to state and prove.
- Builds an exhaustive search over clear orders for small arrays and checks any proposed greedy against it, including both examples.
- Keeps block sums current with a Fenwick tree or a segment tree so each step costs
O(log n), and uses 64-bit totals. - Tests
k = 1(every one costs1),k = n, and ones in clusters far apart.
Follow-ups
- Part 2: return the sequence of
(l, index_cleared, cost)operations. - Let the operation clear every one in the block at once, paying the block's sum once.
- Change the price to the block's sum after the clear.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
