Challenge - 5 Problems
Minimum Window 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 Java code that finds the minimum window substring containing all characters of another string?
DSA Java
import java.util.*; public class Solution { public static String minWindow(String s, String t) { if (s.length() == 0 || t.length() == 0) return ""; Map<Character, Integer> dictT = new HashMap<>(); for (char c : t.toCharArray()) { dictT.put(c, dictT.getOrDefault(c, 0) + 1); } int required = dictT.size(); int formed = 0; Map<Character, Integer> windowCounts = new HashMap<>(); int l = 0, r = 0; int[] ans = {-1, 0, 0}; while (r < s.length()) { char c = s.charAt(r); windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1); if (dictT.containsKey(c) && windowCounts.get(c).intValue() == dictT.get(c).intValue()) { formed++; } while (l <= r && formed == required) { c = s.charAt(l); if (ans[0] == -1 || r - l + 1 < ans[0]) { ans[0] = r - l + 1; ans[1] = l; ans[2] = r; } windowCounts.put(c, windowCounts.get(c) - 1); if (dictT.containsKey(c) && windowCounts.get(c).intValue() < dictT.get(c).intValue()) { formed--; } l++; } r++; } return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1); } public static void main(String[] args) { System.out.println(minWindow("ADOBECODEBANC", "ABC")); } }
Attempts:
2 left
💡 Hint
Look for the smallest substring that contains all characters A, B, and C.
✗ Incorrect
The minimum window substring containing all characters 'A', 'B', and 'C' in "ADOBECODEBANC" is "BANC" because it is the shortest substring that includes all required characters.
🧠 Conceptual
intermediateUnderstanding the Sliding Window Technique
In the minimum window substring problem, why do we use two pointers (left and right) to create a sliding window over the string?
Attempts:
2 left
💡 Hint
Think about how to find substrings without checking all combinations.
✗ Incorrect
Two pointers help to expand the window to include required characters and contract it to find the smallest valid window efficiently, avoiding checking all substrings.
🔧 Debug
advancedIdentify the Error in Window Count Update
What error will occur if we forget to decrement the count of the left character in the window when moving the left pointer forward in the minimum window substring algorithm?
Attempts:
2 left
💡 Hint
Think about how the window character counts affect the formed variable.
✗ Incorrect
If counts are not decremented when the left pointer moves, the algorithm thinks the window still contains required characters, so it may not contract properly, returning a larger window.
❓ Predict Output
advancedOutput of Minimum Window Substring with Repeated Characters
What is the output of this code snippet?
DSA Java
import java.util.*; public class Solution { public static String minWindow(String s, String t) { if (s.length() == 0 || t.length() == 0) return ""; Map<Character, Integer> dictT = new HashMap<>(); for (char c : t.toCharArray()) { dictT.put(c, dictT.getOrDefault(c, 0) + 1); } int required = dictT.size(); int formed = 0; Map<Character, Integer> windowCounts = new HashMap<>(); int l = 0, r = 0; int[] ans = {-1, 0, 0}; while (r < s.length()) { char c = s.charAt(r); windowCounts.put(c, windowCounts.getOrDefault(c, 0) + 1); if (dictT.containsKey(c) && windowCounts.get(c).intValue() == dictT.get(c).intValue()) { formed++; } while (l <= r && formed == required) { c = s.charAt(l); if (ans[0] == -1 || r - l + 1 < ans[0]) { ans[0] = r - l + 1; ans[1] = l; ans[2] = r; } windowCounts.put(c, windowCounts.get(c) - 1); if (dictT.containsKey(c) && windowCounts.get(c).intValue() < dictT.get(c).intValue()) { formed--; } l++; } r++; } return ans[0] == -1 ? "" : s.substring(ans[1], ans[2] + 1); } public static void main(String[] args) { System.out.println(minWindow("AAABBC", "ABC")); } }
Attempts:
2 left
💡 Hint
Look for the shortest substring containing one A, one B, and one C.
✗ Incorrect
The shortest substring containing 'A', 'B', and 'C' is "ABBC" starting at index 2 to 5.
🧠 Conceptual
expertWhy Use a Hash Map for Character Counts?
Why is a hash map (dictionary) used to store character counts in the minimum window substring problem instead of an array?
Attempts:
2 left
💡 Hint
Think about the variety of characters possible in the input string.
✗ Incorrect
Hash maps allow counting any character including Unicode, while arrays are limited to fixed-size alphabets like ASCII.
