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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Greedy
Sliding Window
Difference Array
Bit Parity
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported