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 JavaScript function call?
DSA Javascript
function minWindow(s, t) {
if (t.length > s.length) return "";
const need = new Map();
for (const c of t) need.set(c, (need.get(c) || 0) + 1);
let left = 0, right = 0, count = t.length, minLen = Infinity, start = 0;
while (right < s.length) {
const c = s[right];
if (need.has(c)) {
if (need.get(c) > 0) count--;
need.set(c, need.get(c) - 1);
}
right++;
while (count === 0) {
if (right - left < minLen) {
minLen = right - left;
start = left;
}
const leftChar = s[left];
if (need.has(leftChar)) {
need.set(leftChar, need.get(leftChar) + 1);
if (need.get(leftChar) > 0) count++;
}
left++;
}
}
return minLen === Infinity ? "" : s.substring(start, start + minLen);
}
console.log(minWindow("ADOBECODEBANC", "ABC"));Attempts:
2 left
💡 Hint
Think about the smallest substring that contains all characters from t.
✗ Incorrect
The minimum window substring containing all characters 'A', 'B', and 'C' in "ADOBECODEBANC" is "BANC".
❓ Predict Output
intermediateResult of Minimum Window Substring with Repeated Characters
What will the function return for this input?
DSA Javascript
function minWindow(s, t) {
if (t.length > s.length) return "";
const need = new Map();
for (const c of t) need.set(c, (need.get(c) || 0) + 1);
let left = 0, right = 0, count = t.length, minLen = Infinity, start = 0;
while (right < s.length) {
const c = s[right];
if (need.has(c)) {
if (need.get(c) > 0) count--;
need.set(c, need.get(c) - 1);
}
right++;
while (count === 0) {
if (right - left < minLen) {
minLen = right - left;
start = left;
}
const leftChar = s[left];
if (need.has(leftChar)) {
need.set(leftChar, need.get(leftChar) + 1);
if (need.get(leftChar) > 0) count++;
}
left++;
}
}
return minLen === Infinity ? "" : s.substring(start, start + minLen);
}
console.log(minWindow("AAABBC", "ABC"));Attempts:
2 left
💡 Hint
Look for the smallest substring containing one 'A', one 'B', and one 'C'.
✗ Incorrect
The smallest substring containing 'A', 'B', and 'C' is "ABBC" starting at index 2.
🔧 Debug
advancedIdentify the Error in Minimum Window Substring Implementation
What error will this code produce when run?
DSA Javascript
function minWindow(s, t) {
const need = new Map();
for (const c of t) need.set(c, (need.get(c) || 0) + 1);
let left = 0, right = 0, count = t.length, minLen = Infinity, start = 0;
while (right < s.length) {
const c = s[right];
if (need.has(c)) {
if (need.get(c) > 0) count--;
need.set(c, need.get(c) - 1);
}
right++;
while (count === 0) {
if (right - left < minLen) {
minLen = right - left;
start = left;
}
const leftChar = s[left];
if (need.has(leftChar)) {
need.set(leftChar, need.get(leftChar) + 1);
if (need.get(leftChar) > 0) count++;
}
left++;
}
}
return minLen === Infinity ? "" : s.substring(start, start + minLen);
}
console.log(minWindow(null, "ABC"));Attempts:
2 left
💡 Hint
Check what happens when s is null and length is accessed.
✗ Incorrect
The code tries to access s.length but s is null, causing a TypeError.
🧠 Conceptual
advancedMinimum Window Substring Algorithm Complexity
What is the time complexity of the standard sliding window algorithm for Minimum Window Substring?
Attempts:
2 left
💡 Hint
Consider how many times the left and right pointers move.
✗ Incorrect
Each character is visited at most twice by left and right pointers, so time complexity is O(n).
🚀 Application
expertFind Minimum Window Substring Containing All Unique Characters
Given the string s = "aabcbcdbca", what is the minimum window substring that contains all unique characters of s?
Attempts:
2 left
💡 Hint
First find all unique characters, then find the smallest substring containing them all.
✗ Incorrect
All unique characters are {a,b,c,d}. The smallest substring containing all is "dbca".
