Challenge - 5 Problems
Minimum Window Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
}Attempts:
2 left
💡 Hint
Think about the smallest substring containing all characters of t.
✗ Incorrect
The minimum window substring containing all characters 'A', 'B', and 'C' in s is "BANC".
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about minimizing the window size while keeping it valid.
✗ Incorrect
After finding a valid window, moving the left pointer forward helps to shrink the window and find the smallest possible valid substring.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Check how 'required' is calculated and used.
✗ Incorrect
The code increments 'required' for every character in t, including duplicates, but 'formed' counts unique characters matched. This mismatch causes the window to never be recognized as valid.
🚀 Application
advanced2:00remaining
Minimum Window Substring with Unicode Characters
How would you modify the Minimum Window Substring algorithm to handle Unicode characters beyond ASCII?
Attempts:
2 left
💡 Hint
Think about character sets larger than 128.
✗ Incorrect
Unicode characters require dynamic data structures like hash maps to count frequencies, as fixed arrays for ASCII are insufficient.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves.
✗ Incorrect
Each character in s is visited at most twice by the two pointers, and t is processed once, resulting in O(n + m) time.
