Maximum Average Subarray I

EasySliding 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 an integer k. Among all contiguous subarrays of length exactly k, find the one with the largest average and return that average. The result is a floating-point number; answers within 1e-6 of the true value are accepted. Aim for O(n) time.

Examples

Input: nums = [1,12,-5,-6,50,3], k = 4
Output: 12.75
The window [12,-5,-6,50] sums to 51, and 51 / 4 = 12.75, the best of all length-4 windows.
Input: nums = [5], k = 1
Output: 5.0
The only window is [5].

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

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