The K Empty Slots interview question is an interval and timing problem. You have bulbs in a row. Each day, one bulb is turned on according to a given sequence. You need to find the earliest day on which there exist two turned-on bulbs that are exactly k positions apart, such that all bulbs between them are currently off.
Google uses the K Empty Slots coding problem to evaluate a candidate's ability to handle Sliding Window and Ordered Set logic. It requires you to track neighbors in a dynamic set (which bulbs are on) and check distances. It evaluations your proficiency with Segment Trees or Binary Indexed Trees if you choose a query-based approach.
This problem can be solved using a Sliding Window on the "Days" array.
days where days[i] is the day the bulb at position i is turned on.[i, i + k + 1] such that days[i] and days[i + k + 1] are both less than all days[j] for in the middle.TreeSet) to store positions of turned-on bulbs. For each new bulb, check its successor and predecessor in the set.Bulbs: [1, 3, 2], .
set = {1}.set = {1, 3}.
Master the Sliding Window Minimum/Maximum pattern. Being able to check if all values in a range are greater than some threshold in or is a critical skill for high-level Array interview patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Continuous Subarrays | Medium | Solve | |
| Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Medium | Solve | |
| Max Value of Equation | Hard | Solve | |
| Sliding Window Maximum | Hard | Solve | |
| Constrained Subsequence Sum | Hard | Solve |