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 C# code that finds the minimum window substring?
DSA C#
using System; using System.Collections.Generic; class Program { static string MinWindow(string s, string t) { if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return ""; Dictionary<char, int> dictT = new Dictionary<char, int>(); foreach (char c in t) { dictT[c] = dictT.GetValueOrDefault(c, 0) + 1; } int required = dictT.Count; int l = 0, r = 0, formed = 0; Dictionary<char, int> windowCounts = new Dictionary<char, int>(); int[] ans = {-1, 0, 0}; while (r < s.Length) { char c = s[r]; windowCounts[c] = windowCounts.GetValueOrDefault(c, 0) + 1; if (dictT.ContainsKey(c) && windowCounts[c] == dictT[c]) { formed++; } while (l <= r && formed == required) { c = s[l]; if (ans[0] == -1 || r - l + 1 < ans[0]) { ans[0] = r - l + 1; ans[1] = l; ans[2] = r; } windowCounts[c]--; if (dictT.ContainsKey(c) && windowCounts[c] < dictT[c]) { formed--; } l++; } r++; } return ans[0] == -1 ? "" : s.Substring(ans[1], ans[0]); } static void Main() { string s = "ADOBECODEBANC"; string t = "ABC"; Console.WriteLine(MinWindow(s, t)); } }
Attempts:
2 left
π‘ Hint
Think about the smallest substring in s that contains all characters of t.
β Incorrect
The minimum window substring that contains all characters 'A', 'B', and 'C' in 'ADOBECODEBANC' is 'BANC'.
β Predict Output
intermediateOutput when target string has repeated characters
What is the output of the following C# code when the target string has repeated characters?
DSA C#
using System; using System.Collections.Generic; class Program { static string MinWindow(string s, string t) { if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t)) return ""; Dictionary<char, int> dictT = new Dictionary<char, int>(); foreach (char c in t) { dictT[c] = dictT.GetValueOrDefault(c, 0) + 1; } int required = dictT.Count; int l = 0, r = 0, formed = 0; Dictionary<char, int> windowCounts = new Dictionary<char, int>(); int[] ans = {-1, 0, 0}; while (r < s.Length) { char c = s[r]; windowCounts[c] = windowCounts.GetValueOrDefault(c, 0) + 1; if (dictT.ContainsKey(c) && windowCounts[c] == dictT[c]) { formed++; } while (l <= r && formed == required) { c = s[l]; if (ans[0] == -1 || r - l + 1 < ans[0]) { ans[0] = r - l + 1; ans[1] = l; ans[2] = r; } windowCounts[c]--; if (dictT.ContainsKey(c) && windowCounts[c] < dictT[c]) { formed--; } l++; } r++; } return ans[0] == -1 ? "" : s.Substring(ans[1], ans[0]); } static void Main() { string s = "AAABBC"; string t = "AABC"; Console.WriteLine(MinWindow(s, t)); } }
Attempts:
2 left
π‘ Hint
Remember the target has two 'A's, one 'B', and one 'C'.
β Incorrect
The smallest substring containing two 'A's, one 'B', and one 'C' in 'AAABBC' is 'AABBC'.
π§ Debug
advancedIdentify the error in the Minimum Window Substring code
What error will the following C# code produce when run?
DSA C#
using System; using System.Collections.Generic; class Program { static string MinWindow(string s, string t) { Dictionary<char, int> dictT = new Dictionary<char, int>(); foreach (char c in t) { dictT[c] = dictT.GetValueOrDefault(c, 0) + 1; } int required = dictT.Count; int l = 0, r = 0, formed = 0; Dictionary<char, int> windowCounts = new Dictionary<char, int>(); int[] ans = {-1, 0, 0}; while (r < s.Length) { char c = s[r]; windowCounts[c] = windowCounts.GetValueOrDefault(c, 0) + 1; if (dictT.ContainsKey(c) && windowCounts[c] == dictT[c]) { formed++; } while (l <= r && formed == required) { c = s[l]; if (ans[0] == -1 || r - l + 1 < ans[0]) { ans[0] = r - l + 1; ans[1] = l; ans[2] = r; } windowCounts[c]--; if (dictT.ContainsKey(c) && windowCounts[c] < dictT[c]) { formed--; } l++; } r++; } return ans[0] == -1 ? "" : s.Substring(ans[1], ans[0]); } static void Main() { string s = "ADOBECODEBANC"; string t = "ABC"; Console.WriteLine(MinWindow(s, t)); } }
Attempts:
2 left
π‘ Hint
Check how dictionary keys are accessed and updated.
β Incorrect
The code tries to increment dictT[c] without initializing the key c, causing a KeyNotFoundException.
π§ Conceptual
advancedWhy use two pointers in Minimum Window Substring?
Why does the Minimum Window Substring algorithm use two pointers (left and right) to scan the string?
Attempts:
2 left
π‘ Hint
Think about how the window changes size during the search.
β Incorrect
Two pointers allow the window to grow and shrink, efficiently finding the minimum substring that contains all required characters.
π Application
expertMinimum Window Substring with Unicode Characters
Given the string s = "πΈπΉπΈπ»πΉπΈπ»πΈ" and target t = "πΈπ»πΉ", what is the minimum window substring returned by a correct Minimum Window Substring algorithm that supports Unicode characters?
Attempts:
2 left
π‘ Hint
Unicode characters may be multiple bytes but are treated as single characters in C# strings.
β Incorrect
The smallest substring containing all characters 'πΈ', 'π»', and 'πΉ' is 'πΉπΈπ»' starting at index 3.
