Count Subarrays With Fixed Bounds

HardSliding Windowsliding window

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 needed

Problem

You are given an integer array nums and two integers min_k and max_k. A contiguous subarray is **fixed-bound** when its minimum is exactly min_k and its maximum is exactly max_k. Return the number of fixed-bound subarrays. Subarrays at different positions count separately even if their contents are equal. The count can exceed 32-bit range. Aim for O(n) time and O(1) extra space.

Examples

Input: nums = [1,3,5,2,7,5], min_k = 1, max_k = 5
Output: 2
[1,3,5] and [1,3,5,2] qualify. Anything containing the 7 has a maximum that is too large.
Input: nums = [1,1,1,1], min_k = 1, max_k = 1
Output: 10
All 10 subarrays have min and max equal to 1.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i], min_k, max_k <= 10^6

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.

More sliding window problems