Longest Interval Where Tiring Days Outnumber Non-Tiring Days

Quick Overview

Given the hours an employee worked each day, find the length of the longest run of consecutive days in which days with more than 8 hours outnumber the rest. Tests turning a counting condition into a subarray sum problem and reasoning about running sums in one linear pass.

Longest Interval Where Tiring Days Outnumber Non-Tiring Days

Company: Infosys

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

We are given `hours`, a list of the number of hours worked per day for a given employee. A day is considered to be a **tiring day** if and only if the number of hours worked is (strictly) greater than `8`. A **well-performing interval** is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. Return the length of the longest well-performing interval. ### Function Signature ```python def longest_well_performing_interval(hours: list[int]) -> int: ``` ### Rules - An interval is a contiguous, non-empty range of days `hours[i..j]`. - A day with exactly 8 hours is not tiring. - Return `0` when no well-performing interval exists. ### Constraints - `1 <= len(hours) <= 10^4` - `0 <= hours[i] <= 16` ### Examples ```text Input: hours = [9, 9, 6, 0, 6, 6, 9] Output: 3 ``` The interval `[9, 9, 6]` has 2 tiring days against 1 non-tiring day. Any longer interval needs at least 3 tiring days, and the only interval holding all three (positions 0, 1 and 6) is the whole list, where 4 days are non-tiring. ```text Input: hours = [6, 6, 6] Output: 0 ``` No day is tiring, so no interval qualifies. ```text Input: hours = [6, 6, 9, 9, 6] Output: 3 ``` Days 1 to 3 (`[6, 9, 9]`) and days 2 to 4 (`[9, 9, 6]`) each have 2 tiring days against 1. There are only 2 tiring days in total, so an interval of 4 or more days cannot have more tiring than non-tiring days.

Overview: Given the hours an employee worked each day, find the length of the longest run of consecutive days in which days with more than 8 hours outnumber the rest. Tests turning a counting condition into a subarray sum problem and reasoning about running sums in one linear pass.

Infosys logo
Infosys
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

We are given hours, a list of the number of hours worked per day for a given employee.

A day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8.

A well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.

Return the length of the longest well-performing interval.

Function Signature

def longest_well_performing_interval(hours: list[int]) -> int:

Rules

  • An interval is a contiguous, non-empty range of days hours[i..j] .
  • A day with exactly 8 hours is not tiring.
  • Return 0 when no well-performing interval exists.

Constraints

  • 1 <= len(hours) <= 10^4
  • 0 <= hours[i] <= 16

Examples

Input: hours = [9, 9, 6, 0, 6, 6, 9]
Output: 3

The interval [9, 9, 6] has 2 tiring days against 1 non-tiring day. Any longer interval needs at least 3 tiring days, and the only interval holding all three (positions 0, 1 and 6) is the whole list, where 4 days are non-tiring.

Input: hours = [6, 6, 6]
Output: 0

No day is tiring, so no interval qualifies.

Input: hours = [6, 6, 9, 9, 6]
Output: 3

Days 1 to 3 ([6, 9, 9]) and days 2 to 4 ([9, 9, 6]) each have 2 tiring days against 1. There are only 2 tiring days in total, so an interval of 4 or more days cannot have more tiring than non-tiring days.