Longest Well-Performing Interval
Problem Statement
You are given hours, where hours[i] is how many hours someone worked on day i.
A day is tiring if they worked more than 8 hours. An interval of consecutive days is well-performing if it has strictly more tiring days than non-tiring days.
Return the length of the longest well-performing interval (0 if there is none).
An integer array hours.
The length of the longest interval of consecutive days with more tiring days than non-tiring days.
[9, 9, 6] has 2 tiring days and 1 non-tiring day. No longer interval has more tiring days.
No day is tiring, so no interval can have more tiring days.
- • 1 ≤ hours.length ≤ 10^4
- • 0 ≤ hours[i] ≤ 16
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Fix each start, grow the end with a running ±1 score, check score > 0
If score > 0 take the whole prefix; else pair with the first index of score − 1
For every start day, walk the end to the right. Keep a running score (+1 for a tiring day, −1 otherwise). Whenever the score is above 0, the interval has more tiring days, so update the best length.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.