Longest Interval Where Tiring Days Outnumber Non-Tiring Days
Company: Infosys
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
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.
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
- 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.
- A day with exactly 8 hours is not tiring, and an interval with as many tiring days as non-tiring days does not qualify.
- 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.