Grumpy Bookstore Owner solution explained

Grumpy Bookstore Owner splits into two sums: the customers who are happy anyway (every minute the owner is calm) and the extra customers a calm streak can win back. The first is a single pass. The second is the largest sum, over any window of the given length, of customers who arrive during grumpy minutes, found with a fixed-size sliding window in O(n) time and O(1) space.

By Published 6 min read

The problem in plain words

A shop is open for n minutes. You get two lists of length n: how many customers come in at each minute, and whether the owner is grumpy at that minute (1) or calm (0). Customers who arrive during a calm minute leave happy; those who arrive during a grumpy minute do not. Once per day the owner can stay calm for a block of minutes consecutive minutes, overriding the grumpy flag inside that block. Return the largest number of happy customers.

Shop days made up for this page; answers from the code below
CustomersGrumpyStreakBestWhy
[2, 6, 1, 3, 5, 4][0, 1, 0, 1, 1, 0]2157 happy anyway; covering minutes 3 and 4 wins back 3 + 5
[3, 3, 3][1, 1, 1]13Always grumpy, so the streak saves one minute's worth
[5, 0, 7][0, 0, 0]212Never grumpy; the streak adds nothing
[1, 8, 1, 8][1, 1, 0, 1]317Minutes 1 to 3 recover 8 + 8 around a calm minute
Shop days made up for this page; answers from the code below. Scroll sideways on a small screen.

What it tests

The hard part is not the window, it is seeing which part of the total depends on your choice. Interviewers look for the split into a fixed base and a variable bonus. Once you say that out loud, the code is a textbook fixed-size window, and the conversation can move to variations like several streaks or a cost per streak.

Brute force: try every streak

def most_happy_slow(customers, grumpy, minutes):
    n = len(customers)
    best = 0
    for start in range(max(1, n - minutes + 1)):
        happy = 0
        for i in range(n):
            calm = grumpy[i] == 0 or start <= i < start + minutes
            if calm:
                happy += customers[i]
        best = max(best, happy)
    return best
For each start, recount the whole day: O(n²).

Every candidate position redoes the same work for the minutes outside the streak. That repeated work is the hint: those minutes contribute the same amount wherever the streak goes, unless they are grumpy.

Base plus best bonus

Write the answer as base + bonus. The base is the sum of customers at calm minutes, which the streak cannot change. The bonus is the sum of customers at grumpy minutes inside the streak, because those are the only ones the streak rescues. Picture a third list, lost[i] = customers[i] if the owner is grumpy at i, else 0. The best bonus is the largest sum of any minutes consecutive entries of lost, which a fixed-size window finds in one pass: add the minute entering on the right, subtract the minute leaving on the left.

def most_happy(customers, grumpy, minutes):
    # customers who are happy no matter what
    base = sum(c for c, g in zip(customers, grumpy) if g == 0)
    # extra customers won back by a calm window: a fixed-size sliding window
    extra = 0
    best_extra = 0
    for i, (c, g) in enumerate(zip(customers, grumpy)):
        if g == 1:
            extra += c
        if i >= minutes and grumpy[i - minutes] == 1:
            extra -= customers[i - minutes]   # minute that just left the window
        best_extra = max(best_extra, extra)
    return base + best_extra
One pass for the base, one window for the bonus: O(n) time, O(1) space.
#include <vector>
#include <algorithm>
using namespace std;

int mostHappy(const vector<int>& customers, const vector<int>& grumpy, int minutes) {
    int base = 0, extra = 0, bestExtra = 0;
    for (int i = 0; i < (int)customers.size(); ++i) {
        if (grumpy[i] == 0) base += customers[i];
        else extra += customers[i];
        if (i >= minutes && grumpy[i - minutes] == 1) extra -= customers[i - minutes];
        bestExtra = max(bestExtra, extra);
    }
    return base + bestExtra;
}
C++17: base and bonus in a single loop.

Taking the maximum before the window is full is safe: a partial window's bonus is never more than the full window that contains it, because all values are non-negative.

Trace on the first example

Customers [2, 6, 1, 3, 5, 4], grumpy [0, 1, 0, 1, 1, 0], streak 2. Base: minutes 0, 2 and 5 are calm, so 2 + 1 + 4 = 7. Lost customers per minute: 0, 6, 0, 3, 5, 0. Windows of two: 6, 6, 3, 8, 5. The best bonus is 8 (minutes 3 and 4), so the answer is 7 + 8 = 15.

Complexity

n minutes
ApproachTimeExtra space
Recount for each streakO(n²)O(1)
Base plus window bonusO(n)O(1)
Prefix sums of lost customersO(n)O(n)
n minutes. Scroll sideways on a small screen.

Edge cases

  • Streak at least as long as the day: every customer is happy, the sum of the whole list.
  • Owner never grumpy: the bonus is 0 and the base is everything.
  • Owner always grumpy: the base is 0 and the answer is the best window of customers.
  • Minutes with zero customers: they cost nothing and change nothing; no special handling needed.

Mistakes that cost points

  • Sliding a window over raw customers instead of lost customers, which counts calm-minute customers twice.
  • Subtracting the leaving minute without checking whether it was grumpy.
  • Only comparing full windows and then mishandling a streak longer than the day.
  • Returning just the bonus and forgetting the base.

Follow-ups you might get

  • Two separate, non-overlapping streaks. Compute the best window ending at or before each minute and the best starting at or after it, then try every split point: still O(n).
  • The streak has a cost per minute. It becomes a window whose length you choose, which turns into a maximum-sum subarray (Kadane-style) on lost[i] minus the cost.
  • Report which minutes to cover. Track the end index when best_extra improves.

How to explain it out loud

Fictional example

A concise spoken walkthrough
Candidate
Customers in calm minutes are happy wherever the streak goes, so I'll add them up once as a base.
Candidate
The streak only matters for grumpy minutes. I want the window of that length with the most grumpy-minute customers: a fixed-size sliding window, add on the right, remove on the left.
Candidate
Answer is base plus best window. Linear time, constant space.

Maximum Points You Can Obtain from Cards has the same "fixed total plus best window" flavor. Minimum Swaps to Group All 1's Together slides a window whose length comes from the data, and Get Equal Substrings Within Budget lets the window grow and shrink. Browse the rest on the coding problems hub.

Rehearsing with Natively

Natively runs on macOS and Windows, and during practice it can read the shop problem from a screenshot and suggest where to split base from bonus, or point out a missing grumpy check in your C++. Reviewing the saved transcript afterward shows how long you took to state the split. See the AI interview assistant page for setup.

Questions people ask

How do you solve Grumpy Bookstore Owner?

Add up customers at calm minutes as a base. Then slide a window of the streak length over customers at grumpy minutes and keep the largest window sum. The answer is base plus that best window.

What is the time complexity of Grumpy Bookstore Owner?

O(n) time and O(1) extra space with the fixed-size sliding window. Trying every streak position and recounting is O(n²).

Why do you only slide over grumpy minutes in Grumpy Bookstore Owner?

Customers in calm minutes are happy wherever the streak goes, so they do not depend on the choice. Only grumpy-minute customers change with the streak's position.

What pattern is Grumpy Bookstore Owner?

A fixed-length sliding window over a filtered list: the customers lost to grumpy minutes.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP