Fewest K-Length Toggles to Turn Every Light in a Smart Home On
Last updated: November 29, 2026
Quick Overview
A row of Google Home controlled lights is given as 0 (off) and 1 (on). One operation toggles exactly k consecutive lights. Return the minimum number of operations to turn every light on, or -1 if impossible.
0
0
0 solved
A row of Google Home controlled lights is given as 0 (off) and 1 (on). One operation toggles exactly k consecutive lights. Return the minimum number of operations to turn every light on, or -1 if impossible.
A Google onsite Hard problem that combines a greedy argument with a sliding window of active flips. The interviewer checks that the candidate proves the leftmost off light forces a flip and then tracks flip parity in O(1) per position instead of toggling k elements each time.
What the Interviewer Expects
- Argues the leftmost off light must be flipped by the window starting there
- Tracks the parity of flips covering the current position
- Expires flips that started k positions ago
- Returns -1 when a needed flip would run past the end
- States O(n) time
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
- How would you do this with O(1) extra space by marking the input?
- What if toggles can have any length up to k?
- What if the lights form a ring?
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
lightsof 0s and 1s, integerk >= 1. - Operation: toggle
lights[i..i+k-1]for someiwithi + k <= n. - Output: minimum number of operations, or -1....
High-Level Design
Order of flips does not matter and flipping the same window twice cancels, so each start index is used at most once. Scan left to right. The leftmost light that is currently off can only be fixed by a...