Asked at 10 companies
6 recent
- tcs
- Meta
- Bloomberg
- Amazon
- Microsoft
- Yandex
- Infosys
- Uber
- Goldman Sachs
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Counting with a hash map · A set for “have I seen this?”
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · API rate limits: “100 requests per minute” is a sliding window: count what falls inside the last 60 seconds, drop what slides out, and reject the extra.
ProblemProblem statementYou are given an integer array nums consisting of n elements, and an integer k.ReadHide
Problem Statement
You are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10^-5 will be accepted.
Added 2026-11-08 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: nums = [1,12,-5,-6,50,3], k = 4
Output: 12.75
Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75.
Input: nums = [5], k = 1
Output: 5.0
Input: nums = [0,1,1,3,3], k = 4
Output: 2.0
Explanation: Maximum average is (1 + 1 + 3 + 3) / 4 = 8 / 4 = 2.0.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Fixed-size sliding window — every candidate has exactly length k, so the window never grows or shrinks, it only slides. The sum of the next window is the old sum minus the element leaving and plus the element entering. And since dividing by k is the same for every window, comparing sums is the same as comparing averages — track the max sum and divide once at the end.
Approach
Brute Force — Recompute Every Window's Sum, O(n * k)
best = -inf
for i in range(n - k + 1):
best = max(best, sum(nums[i:i+k]) / k)
return best
Each of the ~n windows costs O(k) to sum from scratch.
Optimized — Sliding Window Sum, O(n)
window = sum(nums[0:k])
best = window
for i in range(k, n):
window += nums[i] - nums[i - k] # one in, one out
best = max(best, window)
return best / k
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Recompute every sum | O(n*k) | O(1) | Sums each window from scratch |
| Sliding window sum | O(n) | O(1) | One element in, one element out |
Solution code
class SolutionBruteForce:
"""O(n * k) time | O(1) space — recompute every window's sum."""
def findMaxAverage(self, nums, k: int) -> float:
best = float("-inf")
for i in range(len(nums) - k + 1):
best = max(best, sum(nums[i:i + k]) / k)
return best
class Solution:
"""O(n) time | O(1) space — sliding window sum, divide once at the end."""
def findMaxAverage(self, nums, k: int) -> float:
window = sum(nums[:k])
best = window
for i in range(k, len(nums)):
window += nums[i] - nums[i - k]
best = max(best, window)
return best / kOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language