Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Sliding Window on Arrays in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the sum of the first window of size k in the array.

DSA Typescript
function initialWindowSum(arr: number[], k: number): number {
  let sum = 0;
  for (let i = 0; i < [1]; i++) {
    sum += arr[i];
  }
  return sum;
}
Drag options to blanks, or click blank then click option'
Ak
Barr.length
Ck - 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr.length instead of k causes summing the whole array.
Using k - 1 causes missing the last element in the window.
2fill in blank
medium

Complete the code to slide the window by one position and update the sum.

DSA Typescript
function slideWindowSum(arr: number[], start: number, end: number, currentSum: number): number {
  return currentSum - arr[[1]] + arr[end];
}
Drag options to blanks, or click blank then click option'
Aend
Bstart
Cstart + 1
Dend - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting arr[end] instead of arr[start] causes wrong sum.
Using start + 1 or end - 1 causes index errors.
3fill in blank
hard

Fix the error in the code to correctly find the maximum sum of any window of size k.

DSA Typescript
function maxWindowSum(arr: number[], k: number): number {
  let maxSum = 0;
  let windowSum = 0;
  for (let i = 0; i < k; i++) {
    windowSum += arr[i];
  }
  maxSum = windowSum;
  for (let i = k; i < arr.length; i++) {
    windowSum = windowSum - arr[i - k] + arr[[1]];
    if (windowSum > maxSum) {
      maxSum = windowSum;
    }
  }
  return maxSum;
}
Drag options to blanks, or click blank then click option'
Ai
Bi - 1
Ci + 1
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 or i + 1 causes off-by-one errors.
Using k as index causes wrong element access.
4fill in blank
hard

Fill both blanks to create a function that returns an array of sums of all windows of size k.

DSA Typescript
function allWindowSums(arr: number[], k: number): number[] {
  const result: number[] = [];
  let windowSum = 0;
  for (let i = 0; i < [1]; i++) {
    windowSum += arr[i];
  }
  result.push(windowSum);
  for (let i = k; i < arr.length; i++) {
    windowSum = windowSum - arr[i - [2]] + arr[i];
    result.push(windowSum);
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Ak
B1
Ck - 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using k - 1 instead of k causes wrong index for subtraction.
Using 1 or 0 causes incorrect window size handling.
5fill in blank
hard

Fill all three blanks to implement a function that finds the minimum sum of any window of size k.

DSA Typescript
function minWindowSum(arr: number[], k: number): number {
  let minSum = 0;
  let windowSum = 0;
  for (let i = 0; i < [1]; i++) {
    windowSum += arr[i];
  }
  minSum = windowSum;
  for (let i = [2]; i < arr.length; i++) {
    windowSum = windowSum - arr[i - k] + arr[[3]];
    if (windowSum < minSum) {
      minSum = windowSum;
    }
  }
  return minSum;
}
Drag options to blanks, or click blank then click option'
Ak
Ci
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the second loop at 0 causes index errors.
Using wrong index for adding new element causes wrong sums.

Practice

(1/5)
1. What is the main purpose of the sliding window technique on arrays?
easy
A. To find the maximum element in the entire array
B. To sort the array in ascending order
C. To reverse the elements of the array
D. To efficiently process subarrays of fixed size without repeated work

Solution

  1. Step 1: Understand the sliding window concept

    The sliding window technique moves a fixed-size window over the array to process subarrays efficiently.
  2. Step 2: Identify the purpose

    It avoids recalculating values for overlapping parts of subarrays, improving performance.
  3. Final Answer:

    To efficiently process subarrays of fixed size without repeated work -> Option D
  4. Quick Check:

    Sliding window = efficient fixed-size subarray processing [OK]
Hint: Think of a moving window over array parts for efficiency [OK]
Common Mistakes:
  • Confusing sliding window with sorting
  • Thinking it reverses the array
  • Assuming it finds max of whole array
2. Which of the following is the correct TypeScript syntax to start a sliding window loop for an array arr with window size k?
easy
A. for (let i = 0; i < arr.length; i += k)
B. for (let i = 0; i < arr.length + k; i++)
C. for (let i = 0; i <= arr.length - k; i++)
D. for (let i = 1; i <= arr.length - k; i++)

Solution

  1. Step 1: Determine loop range for sliding window

    The window of size k must fit inside the array, so the last start index is arr.length - k.
  2. Step 2: Check loop syntax correctness

    Loop from i = 0 up to and including arr.length - k ensures all windows are covered.
  3. Final Answer:

    for (let i = 0; i <= arr.length - k; i++) -> Option C
  4. Quick Check:

    Loop runs from 0 to arr.length - k inclusive [OK]
Hint: Loop until index allows full window inside array [OK]
Common Mistakes:
  • Using < instead of <= causing last window to miss
  • Starting loop at 1 instead of 0
  • Incrementing by k skipping windows
3. What is the output of this TypeScript code?
const arr = [1, 3, 2, 6, -1, 4, 1, 8, 2];
const k = 5;
const result: number[] = [];
let windowSum = 0;

for (let i = 0; i < k; i++) {
  windowSum += arr[i];
}
result.push(windowSum);

for (let i = k; i < arr.length; i++) {
  windowSum += arr[i] - arr[i - k];
  result.push(windowSum);
}

console.log(result);
medium
A. [11, 14, 12, 18, 14]
B. [11, 10, 12, 15, 13]
C. [11, 14, 17, 20, 23]
D. [11, 13, 15, 17, 19]

Solution

  1. Step 1: Calculate initial window sum for first 5 elements

    1 + 3 + 2 + 6 + (-1) = 11. result = [11]
  2. Step 2: Slide window and update sums

    i=5: 11 - 1 + 4 = 14
    i=6: 14 - 3 + 1 = 12
    i=7: 12 - 2 + 8 = 18
    i=8: 18 - 6 + 2 = 14
    result = [11, 14, 12, 18, 14]
  3. Final Answer:

    [11, 14, 12, 18, 14] -> Option A
  4. Quick Check:

    windowSum += arr[i] - arr[i - k] each slide [OK]
Hint: Add new element, subtract old element for each slide [OK]
Common Mistakes:
  • Not subtracting the element leaving the window
  • Incorrect initial sum calculation
  • Off-by-one errors in loop indices
4. Identify the error in this sliding window code snippet and choose the fix:
const arr = [2, 1, 5, 1, 3, 2];
const k = 3;
let maxSum = 0;
let windowSum = 0;

for (let i = 0; i < k; i++) {
  windowSum += arr[i];
}
maxSum = windowSum;

for (let i = k; i < arr.length; i++) {
  windowSum = windowSum + arr[i] - arr[i + k];
  if (windowSum > maxSum) {
    maxSum = windowSum;
  }
}
console.log(maxSum);
medium
A. Initialize maxSum to Number.MIN_VALUE
B. Change arr[i + k] to arr[i - k] in the second loop
C. Change first loop to run from i = 1 to k
D. Add a check if i + k < arr.length before subtraction

Solution

  1. Step 1: Analyze the sliding window update

    The window slides by removing the element leaving the window and adding the new element entering it. The element leaving is at index i - k, not i + k.
  2. Step 2: Correct the subtraction index

    Change arr[i + k] to arr[i - k] to correctly remove the leftmost element of the previous window.
  3. Final Answer:

    Change arr[i + k] to arr[i - k] in the second loop -> Option B
  4. Quick Check:

    Subtract element leaving window at i-k [OK]
Hint: Subtract element at i-k, not i+k, when sliding window [OK]
Common Mistakes:
  • Using wrong index for element leaving window
  • Not initializing maxSum correctly
  • Loop range errors
5. Given an array of integers, write a sliding window algorithm to find the maximum sum of any subarray of size k. Which approach below correctly implements this in TypeScript?
hard
A. Initialize sum of first k elements, then slide window adding new element and subtracting old, updating max sum each time
B. Sort the array and sum the first k elements
C. Use nested loops to sum every subarray of size k and track max sum
D. Add all elements and divide by k to get average, then multiply by k for max sum

Solution

  1. Step 1: Understand the sliding window max sum approach

    Calculate sum of first k elements as initial window sum.
  2. Step 2: Slide window across array

    For each new element, add it and subtract the element leaving the window, update max sum if current sum is higher.
  3. Step 3: Avoid inefficient methods

    Sorting or nested loops are less efficient; averaging does not find max sum.
  4. Final Answer:

    Initialize sum of first k elements, then slide window adding new element and subtracting old, updating max sum each time -> Option A
  5. Quick Check:

    Sliding window updates sum and tracks max [OK]
Hint: Slide window, update sum and max in one pass [OK]
Common Mistakes:
  • Using nested loops causing slow performance
  • Sorting array which changes element order
  • Calculating average instead of max sum
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard