FastPrepGrumpy Owner's Calm Stretch
Problems/Grumpy Owner's Calm Stretch

Grumpy Owner's Calm Stretch

Nutanix logoNutanix● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2019 Nutanix intern report named the second question of its coding challenge only as a grumpy broker problem. It said this is a known practice problem. It points to the well-known puzzle about a grumpy shop owner who can stay calm for a few minutes in a row.

The report gives no statement, so this version uses the usual setup. Each minute's mood is given as a 0 or 1 flag, where 1 means a bad mood. Visitors who come in during a bad-mood minute leave unhappy, and everyone else leaves happy. The owner can stay calm once, for exactly k minutes in a row inside the day. The block cannot wrap from the end of the day back to the start. The task asks for the largest possible number of happy visitors. The limits are our choice: the day has 1 to 10^5 minutes, k is from 1 to n, and each minute has 0 to 1000 visitors.

Our version is about 80% the same as the reported question.

A shop is open for n minutes, numbered 0 to n - 1. During minute i, visitors[i] people come in, and they all leave at the end of that minute.

The owner's mood changes from minute to minute. moody[i] = 1 means the owner is in a bad mood during minute i, and every visitor of that minute leaves unhappy. moody[i] = 0 means every visitor of that minute leaves happy.

Once during the day, the owner can stay calm for exactly k minutes in a row. The calm block covers minutes s to s + k - 1 for some start s with 0 <= s <= n - k, so it cannot wrap from the end of the day back to the start. During the calm block, every visitor leaves happy, even in minutes where moody[i] = 1.

Return the largest possible number of happy visitors over the whole day.

Function

maxHappyVisitors(visitors: int[], moody: int[], k: int) → int

Examples

Example 1

visitors = [2,0,5,1,3,4]moody = [0,1,1,0,1,0]k = 2return = 12

Without calming down, the happy visitors come in minutes 0, 3 and 5: 2 + 1 + 4 = 7.

A calm block of 2 minutes also saves the visitors of the bad-mood minutes inside it. Starting at minute 1 saves 0 + 5 = 5, starting at minute 2 saves 5, and starting at minute 0, 3 or 4 saves 0, 3 or 3. The best block saves 5, so the answer is 7 + 5 = 12.

Example 2

visitors = [9,0,0,5,5,5]moody = [1,0,0,1,1,1]k = 3return = 15

Every visitor comes in during a bad-mood minute, so nobody is happy without the calm block.

A block covering minutes 0 to 2 saves only the 9 visitors of minute 0. The block covering minutes 3 to 5 saves 5 + 5 + 5 = 15, which is the most. The busiest single minute is not inside the best block. The answer is 15.

Example 3

visitors = [3,8,2,7]moody = [0,0,0,0]k = 1return = 20

The owner is never in a bad mood, so every visitor is happy and the calm block changes nothing: 3 + 8 + 2 + 7 = 20.

Constraints

  • n == visitors.length == moody.length
  • 1 <= k <= n <= 10^5
  • 0 <= visitors[i] <= 1000
  • moody[i] is 0 or 1

More Nutanix Online Assessment questions

  • Good SubsequencesOA · Seen Jul 2026
  • Min Cost to Connect All PointsOA · Seen Jun 2026
  • Digit Changes for a Multiple of 9OA · Seen Aug 2025
  • Gas Station Loop StartOA · Seen Aug 2025
  • Number of Players (for MTS2)OA · Seen Oct 2024
  • Work Schedules (for MTS2)OA · Seen Oct 2024
  • Minimum Operations to Convert ArrayOA · Seen Apr 2024
  • No Adjacent CharactersOA · Seen Apr 2024
  • Build a BST From a ListOA · Seen Nov 2022
  • Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022

Similar questions at other companies

  • Count Subarrays With Product Equal to KSalesforce · OA
  • Count Servers With No Requests in a Time WindowIBM · OA
  • Reschedule Meetings for Maximum Free TimeFivetran · OA
  • Count Subarrays Matching a Comparison PatternCapital One · OA
  • Count Subarrays with at Least K Equal-Fruit PairsTikTok · OA
  • Consistent LogsRippling · OA
  • Minimum Team Size From Every StartCognizant · OA
  • Minimum of Fixed-Window MaximaJuspay · OA
  • Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OA
  • Maximum Decrypted ScoreMicrosoft · OA
See Nutanix hiring insights
Saved
class Solution {
    public int maxHappyVisitors(int[] visitors, int[] moody, int k) {
        // Write your code here.
    }
}
Checking account…