Fewest K-Length Flips to Turn Every Conveyor Light Green
Last updated: November 29, 2026
Quick Overview
A row of conveyor status lights is given as 0 (red) and 1 (green). One operation flips every light in a contiguous block of exactly k lights. Return the minimum number of operations to make all lights green, or -1 if impossible.
Amazon
November 29, 20260
0
0 solved
A row of conveyor status lights is given as 0 (red) and 1 (green). One operation flips every light in a contiguous block of exactly k lights. Return the minimum number of operations to make all lights green, or -1 if impossible.
An Amazon onsite hard question modeled on a maintenance panel that can only toggle fixed-width banks of lights. The interviewer wants the greedy leftmost-red argument and an O(n) implementation that tracks active flips with a difference array or queue instead of flipping k cells each time.
What the Interviewer Expects
- Argue the leftmost red light forces a flip starting there
- Show the naive greedy is O(n * k)
- Track the parity of active flips with a sliding window
- Detect impossibility when a flip would run past the end
- Prove optimality of the greedy
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if flips of any length up to k are allowed?
- What if the lights form a circle?
- Return the list of flip start positions.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list lights of 0s and 1s, integer 1 <= k <= n. Output: minimum flips of length-k blocks to make every value 1, or -1....
High-Level Design
Scan left to right. Consider the leftmost red light at index i. No flip starting after i can reach it, and flips starting before i have already been decided, so we must start a flip at i. This greedy ...