Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Max Subarray Sum in Window

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Max Subarray Sum in Window. Start with the requirements on the right.

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.

Problem

An Apple Watch diagnostics pipeline stores sensor readings in chronological order. Given an integer array readings and a window size k, find the maximum sum among all contiguous windows containing exactly k readings.

Formal Specification

Implement a function that accepts:

  • readings: a list of integers representing sensor readings.
  • k: a positive integer window size.

Return an integer containing the largest sum of any k consecutive elements. The window must contain exactly k readings, and its elements must be contiguous. Do not reorder readings.

Constraints

  • 1 <= k <= len(readings)
  • 1 <= len(readings) <= 10^5
  • -10^4 <= readings[i] <= 10^4
  • The result fits in a signed 64-bit integer

Function Signature

def max_sensor_window_sum(readings, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output