Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Max Sum Subarray of Size K
00:00
5 left

Max Sum Subarray of Size K

MediumPython

Problem

Implement a function to find the maximum sum of a subarray of size K.

Use def max_subarray_sum(nums, k):; return one integer representing the greatest sum of any contiguous window containing exactly k elements. Assume nums contains at least k integers and 1 <= k <= len(nums).

Constraints

  • 1 <= k <= len(nums)
  • 1 <= len(nums) <= 1000
  • -10^4 <= nums[i] <= 10^4
  • The subarray must contain exactly k contiguous elements.

Function Signature

def max_subarray_sum(nums, k):
Interviewer

Your question is Max Sum Subarray of Size K. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.