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 Go function when called with s = "ADOBECODEBANC" and t = "ABC"?
DSA Go
package main import "fmt" func minWindow(s string, t string) string { need := map[byte]int{} for i := 0; i < len(t); i++ { need[t[i]]++ } missing := len(t) left := 0 start, end := 0, len(s)+1 for right := 0; right < len(s); right++ { if need[s[right]] > 0 { missing-- } need[s[right]]-- for missing == 0 { if right-left+1 < end-start { start, end = left, right+1 } need[s[left]]++ if need[s[left]] > 0 { missing++ } left++ } } if end > len(s) { return "" } return s[start:end] } func main() { fmt.Println(minWindow("ADOBECODEBANC", "ABC")) }
Attempts:
2 left
💡 Hint
Focus on the smallest substring containing all characters of t.
✗ Incorrect
The function finds the smallest substring in s that contains all characters of t. The substring "BANC" is the shortest that contains 'A', 'B', and 'C'.
🧠 Conceptual
intermediateUnderstanding the Sliding Window Technique
In the Minimum Window Substring problem, why do we move the left pointer forward only when the current window contains all characters of t?
Attempts:
2 left
💡 Hint
Think about minimizing the substring length.
✗ Incorrect
Moving the left pointer forward when all characters are included helps find the smallest possible window that still contains all required characters.
❓ Predict Output
advancedOutput with Repeated Characters in t
What is the output of the minWindow function when called with s = "AAABBC" and t = "AABC"?
DSA Go
package main import "fmt" func minWindow(s string, t string) string { need := map[byte]int{} for i := 0; i < len(t); i++ { need[t[i]]++ } missing := len(t) left := 0 start, end := 0, len(s)+1 for right := 0; right < len(s); right++ { if need[s[right]] > 0 { missing-- } need[s[right]]-- for missing == 0 { if right-left+1 < end-start { start, end = left, right+1 } need[s[left]]++ if need[s[left]] > 0 { missing++ } left++ } } if end > len(s) { return "" } return s[start:end] } func main() { fmt.Println(minWindow("AAABBC", "AABC")) }
Attempts:
2 left
💡 Hint
Remember t has two 'A's, one 'B', and one 'C'.
✗ Incorrect
The minimum window must contain two 'A's, one 'B', and one 'C'. The substring "AABBC" is the smallest that satisfies this.
❓ Predict Output
advancedOutput When t Contains Characters Not in s
What does the minWindow function return when s = "XYZ" and t = "ABC"?
DSA Go
package main import "fmt" func minWindow(s string, t string) string { need := map[byte]int{} for i := 0; i < len(t); i++ { need[t[i]]++ } missing := len(t) left := 0 start, end := 0, len(s)+1 for right := 0; right < len(s); right++ { if need[s[right]] > 0 { missing-- } need[s[right]]-- for missing == 0 { if right-left+1 < end-start { start, end = left, right+1 } need[s[left]]++ if need[s[left]] > 0 { missing++ } left++ } } if end > len(s) { return "" } return s[start:end] } func main() { fmt.Println(minWindow("XYZ", "ABC")) }
Attempts:
2 left
💡 Hint
If s does not contain all characters of t, what should be returned?
✗ Incorrect
Since s does not contain any characters from t, no valid window exists, so the function returns an empty string.
🧠 Conceptual
expertTime Complexity of Minimum Window Substring Algorithm
What is the time complexity of the sliding window algorithm used in 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
The algorithm uses two pointers that each move forward through the string at most once, resulting in linear time complexity O(n).
