Question Bank

Sliding Window Essentials

Difficulty: Medium

Five mid-level prompts on fixed and variable sliding windows: max sum, longest substring without repeats, and a window-shrink invariant. Mixes implementation and trace.

Question Bank
/

Sliding Window Essentials

Sliding Window Essentials

Five mid-level prompts on fixed and variable sliding windows: max sum, longest substring without repeats, and a window-shrink invariant. Mixes implementation and trace.

Question Bank
Medium
JavaScript
5 questions
sliding-window
algorithms
interview-prep

621 views

6

Implement maxSubarraySum(arr, k) returning the largest sum of any contiguous subarray of length k. Aim for O(n) time and O(1) extra space.

Examples

Example 1:

Input: arr = [2, 1, 5, 1, 3, 2], k = 3
Output: 9
Explanation: The first window [2, 1, 5] sums to 8. Slide one step: add 1, drop 2, sum = 7. Continue sliding: 7, 9, 6. Track the running max. The window [5, 1, 3] gives the maximum 9.