Quiz on Sliding window Technique for DSA

This quiz on Sliding Window Technique will challenge your understanding on the concept of sliding window technique. This technique is widely used to solve many problem like array manipulation, dynamic window resizing, and optimization.

Last Updated :
Discuss
Comments

Question 1

For what purpose we can use sliding window technique in the context of algorithms?

  • To calculate all possible subsets of a given set

  • To optimize the time complexity of finding the maximum or minimum sum in a subarray

  • To implement dynamic programming efficiently

  • To iterate over all elements in an array in constant time

Question 2

Which of the following is the time complexity of using the sliding window technique to find the maximum sum of any subarray of size k in an array of size n?

  • O(n^2)

  • O(n)

  • O(k * n)

  • O(k)

Question 3

What is the main advantage of using the sliding window technique over a brute-force approach?

  • The sliding window technique is always faster

  • The sliding window technique reduces the time complexity by avoiding repeated work

  • The sliding window technique is less memory intensive

  • The sliding window technique works only on arrays with unique elements

Question 4

You are given the following array arr = [1, 2, 3, 4, 5, 6] and the window size k = 3. Initially, the start = 0 and end = 2 (the window includes elements from index 0 to 2). After processing this window, what is the next action taken by the sliding window algorithm?

  • Add the element at index 3 to the window and remove the element at index 0

  • Add the element at index 4 to the window and remove the element at index 1

  • Add the element at index 4 to the window and keep the element at index 2

  • Add the element at index 3 to the window and keep the element at index 0

Question 5

For the given code:

C++
int findMaxSum(vector<int>& arr, int k) {
    int maxSum = 0;
    int windowSum = 0;

    // Calculate the sum of the first window
    for (int i = 0; i < k; i++) {
        windowSum += arr[i];
    }
    maxSum = windowSum;

    // Slide the window and update the sum
    for (int i = k; i < arr.size(); i++) {
        windowSum = windowSum - arr[i - k] + arr[i];
        maxSum = max(maxSum, windowSum);
    }
    return maxSum;
}
C
#include <stdio.h>

int findMaxSum(int arr[], int k) {
    int maxSum = 0;
    int windowSum = 0;
    int n = sizeof(arr)/sizeof(arr[0]); 

    // Calculate the sum of the first window
    for (int i = 0; i < k; i++) {
        windowSum += arr[i];
    }
    maxSum = windowSum;

    // Slide the window and update the sum
    for (int i = k; i < n; i++) {
        windowSum = windowSum - arr[i - k] + arr[i];
        maxSum = (maxSum > windowSum) ? maxSum : windowSum;
    }
    return maxSum;
}
Java
public class MaxSum {
    public static int findMaxSum(int[] arr, int k) {
        int maxSum = 0;
        int windowSum = 0;

        // Calculate the sum of the first window
        for (int i = 0; i < k; i++) {
            windowSum += arr[i];
        }
        maxSum = windowSum;

        // Slide the window and update the sum
        for (int i = k; i < arr.length; i++) {
            windowSum = windowSum - arr[i - k] + arr[i];
            maxSum = Math.max(maxSum, windowSum);
        }
        return maxSum;
    }
}
Python
def findMaxSum(arr, k):
    max_sum = 0
    window_sum = sum(arr[:k])

    for i in range(k, len(arr)):
        window_sum = window_sum - arr[i - k] + arr[i]
        max_sum = max(max_sum, window_sum)

    return max_sum
JavaScript
function findMaxSum(arr, k) {
    let maxSum = 0;
    let windowSum = 0;

    // Calculate the sum of the first window
    for (let i = 0; i < k; i++) {
        windowSum += arr[i];
    }
    maxSum = windowSum;

    // Slide the window and update the sum
    for (let i = k; i < arr.length; i++) {
        windowSum = windowSum - arr[i - k] + arr[i];
        maxSum = Math.max(maxSum, windowSum);
    }

    return maxSum;
}

What is the output of the above code when arr = [2, 1, 5, 1, 3, 2] and k = 3?

  • 9

  • 6

  • 10

  • 7

Question 6

For the given code:

C++
int longestSubstringWithKDistinct(string str, int k) {
    int windowStart = 0, maxLength = 0;
    unordered_map<char, int> freqMap;
    
    for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
        freqMap[str[windowEnd]]++;
        
        while (freqMap.size() > k) {
            freqMap[str[windowStart]]--;
            if (freqMap[str[windowStart]] == 0) {
                freqMap.erase(str[windowStart]);
            }
            windowStart++;
        }
        
        maxLength = max(maxLength, windowEnd - windowStart + 1);
    }
    
    return maxLength;
}
C
#define MAX_CHAR 256

int longestSubstringWithKDistinct(char *str, int k) {
    int windowStart = 0, maxLength = 0;
    int freqMap[MAX_CHAR] = {0};
    int distinctCount = 0;
    
    for (int windowEnd = 0; str[windowEnd] != '\0'; windowEnd++) {
        // Add the current character to the frequency map
        if (freqMap[str[windowEnd]] == 0) {
            distinctCount++;
        }
        freqMap[str[windowEnd]]++;
        
        // Shrink the window if we have more than 'k' distinct characters
        while (distinctCount > k) {
            freqMap[str[windowStart]]--;
            if (freqMap[str[windowStart]] == 0) {
                distinctCount--;
            }
            windowStart++;
        }
        
        // Update maxLength if the current window size is greater
        maxLength = (maxLength > windowEnd - windowStart + 1) ? maxLength : windowEnd - windowStart + 1;
    }
    
    return maxLength;
}
Java
public class LongestSubstring {
    public static int longestSubstringWithKDistinct(String str, int k) {
        int windowStart = 0;
        int maxLength = 0;
        HashMap<Character, Integer> freqMap = new HashMap<>();
        
        for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) {
            freqMap.put(str.charAt(windowEnd), freqMap.getOrDefault(str.charAt(windowEnd), 0) + 1);
            
            while (freqMap.size() > k) {
                freqMap.put(str.charAt(windowStart), freqMap.get(str.charAt(windowStart)) - 1);
                if (freqMap.get(str.charAt(windowStart)) == 0) {
                    freqMap.remove(str.charAt(windowStart));
                }
                windowStart++;
            }
            
            maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
        }
        
        return maxLength;
    }
}
Python
def longestSubstringWithKDistinct(str, k):
    window_start = 0
    max_length = 0
    freq_map = {}
    
    for window_end in range(len(str)):
        freq_map[str[window_end]] = freq_map.get(str[window_end], 0) + 1
        
        while len(freq_map) > k:
            freq_map[str[window_start]] -= 1
            if freq_map[str[window_start]] == 0:
                del freq_map[str[window_start]]
            window_start += 1
        
        max_length = max(max_length, window_end - window_start + 1)
    
    return max_length
JavaScript
function longestSubstringWithKDistinct(str, k) {
    let windowStart = 0;
    let maxLength = 0;
    const freqMap = new Map();
    
    for (let windowEnd = 0; windowEnd < str.length; windowEnd++) {
        freqMap.set(str[windowEnd], (freqMap.get(str[windowEnd]) || 0) + 1);
        
        while (freqMap.size > k) {
            freqMap.set(str[windowStart], freqMap.get(str[windowStart]) - 1);
            if (freqMap.get(str[windowStart]) === 0) {
                freqMap.delete(str[windowStart]);
            }
            windowStart++;
        }
        
        maxLength = Math.max(maxLength, windowEnd - windowStart + 1);
    }
    
    return maxLength;
}

What is the output of the above code when str= "eceabcaabbcdefggffaabca" and k = 3?

  • 7

  • 8

  • 9

  • 6

Tags:

There are 6 questions to complete.

Take a part in the ongoing discussion