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 TypeScript function call?
Function finds the smallest substring in
Function finds the smallest substring in
s containing all characters of t.DSA Typescript
function minWindow(s: string, t: string): string {
const need = new Map<string, number>();
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
Focus on the smallest substring that contains all characters A, B, and C.
✗ Incorrect
The smallest substring of "ADOBECODEBANC" containing all characters 'A', 'B', and 'C' is "BANC".
🧠 Conceptual
intermediateMinimum Window Substring Sliding Window Concept
In the sliding window approach for Minimum Window Substring, what does the variable
count typically represent?Attempts:
2 left
💡 Hint
Think about what needs to be tracked to know when the window contains all required characters.
✗ Incorrect
The variable
count tracks how many characters from the target string are still needed in the current window to satisfy the condition.🔧 Debug
advancedIdentify the Error in Minimum Window Substring Code
What error will the following TypeScript code produce when executed?
DSA Typescript
function minWindow(s: string, t: string): string {
const need = new Map<string, number>();
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);
}
minWindow("a", "a");Attempts:
2 left
💡 Hint
Check how Map.get() is used without null checks.
✗ Incorrect
The code calls need.get(c) without the non-null assertion operator, so TypeScript infers possible undefined, causing runtime TypeError when subtracting from undefined.
❓ Predict Output
advancedResult of Minimum Window Substring with Repeated Characters
What is the output of the following code?
DSA Typescript
console.log(minWindow("aabbccdd", "abc")); function minWindow(s: string, t: string): string { const need = new Map<string, number>(); 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); }
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 optimal sliding window algorithm for the Minimum Window Substring problem, where
n is the length of the source string and m is the length of the target string?Attempts:
2 left
💡 Hint
Consider how many times each pointer moves in the sliding window approach.
✗ Incorrect
Each character in the source string is visited at most twice (once when expanding right pointer, once when moving left pointer), and building the frequency map takes O(m). So total is O(n + m).
