Challenge - 5 Problems
Minimum Window Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Window Substring Function
What is the output of the following C++ code that finds the minimum window substring?
DSA C++
std::string minWindow(std::string s, std::string t) {
std::unordered_map<char, int> dictT;
for (char c : t) dictT[c]++;
int required = dictT.size();
int l = 0, r = 0, formed = 0;
std::unordered_map<char, int> windowCounts;
int minLen = INT_MAX, minLeft = 0;
while (r < s.size()) {
char c = s[r];
windowCounts[c]++;
if (dictT.count(c) && windowCounts[c] == dictT[c]) formed++;
while (l <= r && formed == required) {
c = s[l];
if (r - l + 1 < minLen) {
minLen = r - l + 1;
minLeft = l;
}
windowCounts[c]--;
if (dictT.count(c) && windowCounts[c] < dictT[c]) formed--;
l++;
}
r++;
}
return minLen == INT_MAX ? "" : s.substr(minLeft, minLen);
}
int main() {
std::string s = "ADOBECODEBANC";
std::string t = "ABC";
std::cout << minWindow(s, t) << std::endl;
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 'ADOBECODEBANC' is 'BANC'.
🧠 Conceptual
intermediateUnderstanding the Sliding Window Technique
In the minimum window substring problem, why do we move the left pointer forward when the current window contains all required characters?
Attempts:
2 left
💡 Hint
Think about minimizing the window size while keeping it valid.
✗ Incorrect
Moving the left pointer forward helps shrink the window to the smallest size that still contains all required characters.
🔧 Debug
advancedIdentify the Bug in Window Count Update
What error will occur in this code snippet from the minimum window substring algorithm?
std::unordered_map windowCounts;
windowCounts[s[r]]++;
if (dictT.count(s[r]) && windowCounts[s[r]] == dictT[s[r]]) formed++;
// Later when moving left pointer
windowCounts[s[l]]--;
if (dictT.count(s[l]) && windowCounts[s[l]] < dictT[s[l]]) formed--;
l++;
Attempts:
2 left
💡 Hint
Consider what happens if windowCounts[s[l]] goes below zero.
✗ Incorrect
If windowCounts[s[l]] is decremented below zero, formed may be decremented incorrectly, causing logic errors.
❓ Predict Output
advancedOutput of Minimum Window Substring with Repeated Characters
What is the output of this code snippet?
std::string s = "aabbcc";
std::string t = "abc";
std::cout << minWindow(s, t) << std::endl;
DSA C++
std::string minWindow(std::string s, std::string t) {
std::unordered_map<char, int> dictT;
for (char c : t) dictT[c]++;
int required = dictT.size();
int l = 0, r = 0, formed = 0;
std::unordered_map<char, int> windowCounts;
int minLen = INT_MAX, minLeft = 0;
while (r < s.size()) {
char c = s[r];
windowCounts[c]++;
if (dictT.count(c) && windowCounts[c] == dictT[c]) formed++;
while (l <= r && formed == required) {
c = s[l];
if (r - l + 1 < minLen) {
minLen = r - l + 1;
minLeft = l;
}
windowCounts[c]--;
if (dictT.count(c) && windowCounts[c] < dictT[c]) formed--;
l++;
}
r++;
}
return minLen == INT_MAX ? "" : s.substr(minLeft, minLen);
}Attempts:
2 left
💡 Hint
Look for the smallest substring containing at least one 'a', one 'b', and one 'c'.
✗ Incorrect
The substring "abbc" is the smallest window containing all characters 'a', 'b', and 'c'.
🧠 Conceptual
expertTime Complexity of Minimum Window Substring Algorithm
What is the time complexity of the standard sliding window algorithm for the minimum window substring problem, and why?
Attempts:
2 left
💡 Hint
Consider how many times the left and right pointers move over the string.
✗ Incorrect
Each pointer moves forward at most n times, so total operations are proportional to 2n = O(n).
