Number of Subarrays with Bounded Maximum
MediumTwo Pointerstwo pointers
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an integer array nums and two integers left and right with left <= right.
Count the non-empty contiguous subarrays whose **maximum** element lies in the inclusive range [left, right], and return that count.
Aim for O(n) time.
Examples
Input: nums = [2,1,4,3], left = 2, right = 3
Output: 3
The qualifying subarrays are `[2]`, `[2,1]`, and `[3]`.
Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7
`[2]`, `[2]`, `[2,5]`, `[5]`, `[2,5,6]`, `[5,6]`, and `[6]`; anything containing `9` is too large.
Constraints
- `1 <= nums.length <= 10^5`
- `0 <= nums[i] <= 10^9`
- `0 <= left <= right <= 10^9`
- The answer fits in a 32-bit signed integer
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.