Bird
0
0
DSA Cprogramming~20 mins

Minimum Window Substring in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Minimum Window Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Minimum Window Substring Function
What is the output of the following C function when called with s = "ADOBECODEBANC" and t = "ABC"?
DSA C
char* minWindow(char* s, char* t) {
    int need[128] = {0}, have[128] = {0};
    int required = 0, formed = 0;
    int left = 0, right = 0, start = 0, min_len = 100000;
    for (int i = 0; t[i]; i++) {
        if (need[t[i]] == 0) required++;
        need[t[i]]++;
    }
    while (s[right]) {
        char c = s[right];
        have[c]++;
        if (have[c] == need[c]) formed++;
        while (left <= right && formed == required) {
            if (right - left + 1 < min_len) {
                min_len = right - left + 1;
                start = left;
            }
            char d = s[left];
            have[d]--;
            if (have[d] < need[d]) formed--;
            left++;
        }
        right++;
    }
    if (min_len == 100000) return "";
    char* res = (char*)malloc(min_len + 1);
    strncpy(res, s + start, min_len);
    res[min_len] = '\0';
    return res;
}

int main() {
    char s[] = "ADOBECODEBANC";
    char t[] = "ABC";
    char* result = minWindow(s, t);
    printf("%s\n", result);
    free(result);
    return 0;
}
A"BANC"
B"ADOBEC"
C"CODEBA"
D"DOBECODEBA"
Attempts:
2 left
💡 Hint
Think about the smallest substring containing all characters of t.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Sliding Window Technique
In the Minimum Window Substring problem, why do we move the left pointer forward after finding a valid window?
ATo reset the window and start searching from scratch.
BTo increase the window size to include more characters.
CTo try to find a smaller valid window by removing unnecessary characters from the left.
DTo skip characters that are not in the target string t.
Attempts:
2 left
💡 Hint
Think about minimizing the window size while keeping it valid.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Minimum Window Substring Code
What error will occur when running this code snippet for Minimum Window Substring?
DSA C
char* minWindow(char* s, char* t) {
    int need[128] = {0}, have[128] = {0};
    int required = 0, formed = 0;
    int left = 0, right = 0, start = 0, min_len = 100000;
    for (int i = 0; t[i]; i++) {
        need[t[i]]++;
        required++;
    }
    while (s[right]) {
        char c = s[right];
        have[c]++;
        if (have[c] == need[c]) formed++;
        while (left <= right && formed == required) {
            if (right - left + 1 < min_len) {
                min_len = right - left + 1;
                start = left;
            }
            char d = s[left];
            have[d]--;
            if (have[d] < need[d]) formed--;
            left++;
        }
        right++;
    }
    if (min_len == 100000) return "";
    char* res = (char*)malloc(min_len + 1);
    strncpy(res, s + start, min_len);
    res[min_len] = '\0';
    return res;
}
AMemory leak due to missing free() call for allocated memory.
BThe variable 'required' counts total characters including duplicates, causing incorrect formed count.
CSegmentation fault due to accessing s[right] without checking null terminator.
DSyntax error because of missing semicolon after variable declarations.
Attempts:
2 left
💡 Hint
Check how 'required' is calculated and used.
🚀 Application
advanced
2:00remaining
Minimum Window Substring with Unicode Characters
How would you modify the Minimum Window Substring algorithm to handle Unicode characters beyond ASCII?
AUse a hash map or dictionary to count characters instead of fixed-size arrays.
BIncrease the size of the fixed array to 256 to cover extended ASCII.
CConvert all characters to lowercase ASCII before processing.
DIgnore characters outside ASCII range to simplify the problem.
Attempts:
2 left
💡 Hint
Think about character sets larger than 128.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Minimum Window Substring Algorithm
What is the time complexity of the optimal Minimum Window Substring algorithm using the sliding window technique?
AO(m^2), where m is length of t
BO(n * m), where n is length of s and m is length of t
CO(n^2), where n is length of s
DO(n + m), where n is length of s and m is length of t
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves.