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.

You are given `hours`, a list where `hours[i]` is the number of hours an employee worked on day `i`. A day is a **tiring day** if and only if the number of hours worked that day is strictly greater than `8`. A day with exactly `8` hours is not tiring. An interval is a contiguous, non-empty range of days `hours[i..j]`. An interval is **well-performing** when the number of tiring days in it is strictly larger than the number of non-tiring days in it. An interval with equal counts is not well-performing. Return the length of the longest well-performing interval, or `0` when no well-performing interval exists. The answer lies between `0` and `len(hours)`, so it fits in a 32-bit signed integer. ### Example 1 ```text Input: hours = [9, 9, 6, 0, 6, 6, 9] Output: 3 ``` The interval `[9, 9, 6]` has 2 tiring days and 1 non-tiring day. A longer interval would need all 3 tiring days (positions 0, 1 and 6), and the only interval containing all of them is the whole list, which has 4 non-tiring days. ### Example 2 ```text Input: hours = [6, 6, 9, 9, 6] Output: 3 ``` Both `[6, 9, 9]` (days 1 to 3) and `[9, 9, 6]` (days 2 to 4) have 2 tiring days against 1. The list has only 2 tiring days, so no interval of 4 or more days can qualify. ### Constraints - `1 <= len(hours) <= 10^4` - `0 <= hours[i] <= 16`

Constraints

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

Examples

Input: ([9, 9, 6, 0, 6, 6, 9],)

Expected Output: 3

Explanation: Source example 1: [9, 9, 6] has 2 tiring days against 1, and no longer interval qualifies.

Input: ([6, 6, 6],)

Expected Output: 0

Explanation: Source example 2: no day is tiring, so no interval qualifies.

Hints

  1. Only whether a day is tiring matters, so each day reduces to one of two values. Try +1 for a tiring day and -1 for any other day, then restate the condition on an interval.
  2. A day with exactly 8 hours is not tiring, and an interval with as many tiring days as non-tiring days does not qualify.
  3. With up to 10^4 days, checking every interval one by one is slow. For each possible last day, ask which starting day gives the longest qualifying interval.

Longest Interval Where Tiring Days Outnumber Other Days

mediumInfosysSoftware Engineer

You are given hours, a list where hours[i] is the number of hours an employee worked on day i.

A day is a tiring day if and only if the number of hours worked that day is strictly greater than 8. A day with exactly 8 hours is not tiring.

An interval is a contiguous, non-empty range of days hours[i..j]. An interval is well-performing when the number of tiring days in it is strictly larger than the number of non-tiring days in it. An interval with equal counts is not well-performing.

Return the length of the longest well-performing interval, or 0 when no well-performing interval exists. The answer lies between 0 and len(hours), so it fits in a 32-bit signed integer.

Example 1

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

The interval [9, 9, 6] has 2 tiring days and 1 non-tiring day. A longer interval would need all 3 tiring days (positions 0, 1 and 6), and the only interval containing all of them is the whole list, which has 4 non-tiring days.

Example 2

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

Both [6, 9, 9] (days 1 to 3) and [9, 9, 6] (days 2 to 4) have 2 tiring days against 1. The list has only 2 tiring days, so no interval of 4 or more days can qualify.

Constraints

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

Examples

Example 1
Input
([9, 9, 6, 0, 6, 6, 9],)
Output
3
Notes
Source example 1: [9, 9, 6] has 2 tiring days against 1, and no longer interval qualifies.
Example 2
Input
([6, 6, 6],)
Output
0
Notes
Source example 2: no day is tiring, so no interval qualifies.

Constraints

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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def longest_well_performing_interval(hours: list[int]) -> int:
Sample call
longest_well_performing_interval([9, 9, 6, 0, 6, 6, 9])
Language
Case 1
Input
([9, 9, 6, 0, 6, 6, 9],)
Expected
3
Case 2
Input
([6, 6, 6],)
Expected
0

Browse More Questions

Show the approach

Approach

Map each tiring day to +1 and every other day (including days of exactly 8 hours) to -1. An interval is well-performing exactly when its mapped values sum to a positive number. Let S(i) be the running total after day i, with a total of 0 before the first day. For an interval that ends on day i: if S(i) > 0, the whole prefix ending at i qualifies, and its length i + 1 is the longest possible for that end. Otherwise the interval that starts right after day j qualifies exactly when S(j) < S(i), and the longest one uses the smallest such j. The running total starts at 0 and moves by exactly 1 per day, so it cannot fall below S(i) without first equaling S(i) - 1; the smallest valid j is therefore the first day on which the total equals S(i) - 1. A single left-to-right scan records the first day each non-positive total appears (totals stay between -n and n, so an array indexed by total plus n + 1 is enough) and, for each day, compares i minus that recorded day with the best length so far. An interval with equal tiring and non-tiring counts sums to 0 and never counts. A list with no tiring day never reaches a positive total or a matching earlier total, so the answer is 0; an empty list, which the constraints exclude, also returns 0.

Time complexity:
O(n)
Space complexity:
O(n)