DSA
    intermediate

    Sliding Window

    Sliding Window: Max Sum Subarray

    Calculate the maximum sum of any contiguous subarray of size k. Sliding window reduces repeated calculations.

    ⏱ O(n)
    💾 O(1)
    Ready
    1
    4
    2
    10
    2
    3
    1
    0
    20
    Current Sum
    0
    Max Sum
    0

    Initialize variables: maxSum = 0, windowSum = 0

    Initialize variables: maxSum = 0, windowSum = 0

    50%
    Live Variables
    maxSum0
    windowSum0

    Maximum sum of k elements

    Sequence

    Order the fixed-size sliding window algorithm.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the main benefit of Sliding Window over a naive nested loop?

    Write the code

    Runs your solution

    Max Sum Subarray of Size K

    Given an array of integers and a number K, find the maximum sum of a subarray of size K.

    Example Cases

    Input:[[1, 2, 5, 2, 8, 1, 5], 2]
    Output:10
    Input:[[1, 2, 5, 2, 8, 1, 5], 4]
    Output:17
    Input:[[4, 2, 1, 6], 1]
    Output:6
    javascript

    The short version

    Think of taking a panoramic photo. Your camera can only see a small slice of the landscape at a time. You smoothly slide the camera (window) to the right to capture the next section without losing context.

    In more depth

    Sliding Window

    Many array and string problems ask for the best — or the count — of something over every contiguous block of the input. Recomputing each block from scratch costs O(n·k) and is almost entirely wasted work, because consecutive blocks overlap in all but two elements.

    The idea

    Keep a window over the array and maintain its answer incrementally. Moving the window one step to the right adds one element on the right and removes one on the left, so a running sum, maximum or frequency count updates in O(1) rather than being rebuilt.

    Two forms

    Fixed window — the size k is given. Slide it once across the array: O(n).
    Variable window — grow the right edge while a condition still holds, then shrink the left edge until it holds again. Each index enters and leaves at most once, so this is still O(n) despite the nested loop.

    Recognising it

    The problem says contiguous subarray or substring and asks for a maximum, a minimum, a count, or the longest/shortest block satisfying some property. If the answer for a block can be updated cheaply when one element enters and one leaves, this is the pattern.

    When to reach for this

    • Find the **Longest/Shortest** substring/subarray with a condition
    • Find a window of size K with max/min sum
    • Contiguous section of data

    Interview Questions

    Amazon
    Google
    Facebook
    Microsoft
    Two Sigma
    Uber
    Google
    LinkedIn
    Netflix
    Facebook
    Airbnb
    Two Sigma

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required