Question bankPricingSign in

Maximum Sum Subarray of Size K

Sliding WindowEasy1:00

Given an array of integers and a positive integer k, find the maximum sum of any contiguous subarray of size k.

For example, given nums = [2, 1, 5, 1, 3, 2] and k = 3, the output is 9 because the subarray [5, 1, 3] has the largest sum among all subarrays of size 3.

Explain your approach, why a sliding window is more efficient than brute force, and analyze the time and space complexity.

How to approach it

  • Hint 1

    A brute force approach recalculates the sum for every window from scratch. How much redundant work is that?

  • Hint 2

    When the window slides one position right, only one element enters and one element leaves.

  • Hint 3

    Maintain a running sum: add the new element entering the window and subtract the element leaving.

Ready to answer it out loud?

Record your answer in 1:00 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.

Practising needs an invite code. Join the waitlist and we’ll send you one.