The Grumpy Bookstore Owner interview question is an optimization problem. You are given an array of customers entering a store each minute, and a grumpy array where 1 means the owner is grumpy and 0 means they are not. When grumpy, the customers for that minute are not satisfied. The owner knows a secret technique to keep themselves from being grumpy for minutes consecutive minutes, but it can only be used once. You need to find the maximum number of customers that can be satisfied.
Companies like Amazon, Google, and Microsoft ask this Sliding Window coding problem to test a candidate's ability to separate base state from dynamic state. It evaluates whether you can compute a baseline sum (satisfied customers without the technique) and then use a sliding window to find the maximum "extra" satisfied customers the technique can provide.
This problem uses the Fixed-Size Sliding Window pattern.
customers[i] where grumpy[i] == 0).minutes window (sum of customers[i] where grumpy[i] == 1 in the first window). This is the current_extra.minutes across the array. For each step, add the new minute's unhappy customers to current_extra (if grumpy) and subtract the exiting minute's unhappy customers (if grumpy).max_extra seen.max_extra.customers = [1, 0, 1, 2, 1, 1, 7, 5], grumpy = [0, 1, 0, 1, 0, 1, 0, 1], minutes = 3
customers[1] (since grumpy[1]=1) = 0.customers[3] (grumpy). Extra = .customers[5] + customers[7] = .
Max extra is 6.
Total = Baseline (10) + Max extra (6) = 16.The "Baseline + Max Bonus" separation is a classic optimization trick. If a problem has a guaranteed minimum score and a limited "boost," calculate the guaranteed score in one pass, then use a sliding window to find the optimal place to apply the boost.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Subarrays Where Max Element Appears at Least K Times | Medium | Solve | |
| Minimum Swaps to Group All 1's Together II | Medium | Solve | |
| Find the Power of K-Size Subarrays I | Medium | Solve | |
| Alternating Groups II | Medium | Solve | |
| K Radius Subarray Averages | Medium | Solve |