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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
Sliding Window
Greedy
Difference Array
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
  • 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 Problems
Sample Answer
Requirements
  • Input: list lights of 0s and 1s, integer k >= 1.
  • Operation: toggle lights[i..i+k-1] for some i with i + 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...


Submit Your Answer
Markdown supported