Magicsheet logo

Minimum Window Substring

Hard
42.9%
Updated 6/1/2025

Minimum Window Substring

What is this problem about?

The Minimum Window Substring problem asks you to find the smallest substring of a source string S that contains all characters of a target string T (with at least the required frequencies). This Minimum Window Substring interview question is arguably the most important sliding window problem in coding interviews, appearing on interview lists at dozens of top companies worldwide.

Why is this asked in interviews?

This problem is asked by Apple, Uber, Microsoft, Meta, Amazon, Google, Bloomberg, Airbnb, LinkedIn, Adobe, TikTok, and many more because it is the definitive test of the variable-size sliding window technique with frequency tracking. It combines character counting, two-pointer management, and optimal window shrinking into one cohesive challenge. The hash table, sliding window, and string interview pattern is the cornerstone here.

Algorithmic pattern used

Two-pointer sliding window with frequency maps. Maintain two pointers (left, right). Expand right to add characters to the window; when all characters of T are satisfied (using a counter of "how many distinct T characters are fully covered"), try shrinking from the left. Update the minimum window whenever a valid window is found. Use a hash map for T's character requirements and a window frequency map to track current counts.

Example explanation

S = "ADOBECODEBANC", T = "ABC".

  • Expand right until window covers A, B, C: "ADOBEC" (length 6).
  • Shrink left: remove 'A' → "DOBEC" — missing A. Record "ADOBEC".
  • Continue expanding right: hit 'A' at index 9. "DOBECODEBA" has extra chars. Window = "BANC" eventually (length 4).
  • Can we shrink further? "ANC" — missing B. Stop. Minimum = "BANC" (length 4).

Common mistakes candidates make

  • Using O(n*m) brute force (check all substrings) instead of O(n) sliding window.
  • Not using the "count of fully satisfied characters" optimization — checking all T characters each step is O(|T|) per step.
  • Forgetting to handle duplicate characters in T (need frequency maps, not just sets).
  • Moving the left pointer when no valid window exists yet.

Interview preparation tip

Minimum Window Substring is a must-master problem. The key optimization: maintain a formed counter tracking how many distinct characters from T have their required frequency met in the window. Only when formed == |distinct T chars| do you have a valid window and can shrink. This reduces the validity check from O(|T|) to O(1) per step. Practice this problem until you can implement it cleanly from memory — it appears in virtually every company's interview loop and is used as a bar-setter for string and sliding window mastery.

Similar Questions