Minimum Window Substring
Updated
Problem statement
Given two strings s and t, return the shortest substring of s that contains every character of t, counting repeats. For example, if t has two as, the substring must contain at least two as.
A substring is a run of characters that are next to each other in s. Upper-case and lower-case letters are different characters.
If several shortest substrings exist, return the one that starts first. If no substring works, return the empty string "".
Function
minWindow(s: String, t: String) → StringExamples
Example 1
s = "xaybzcabz"t = "abz"return = "abz"We need a window with at least one a, one b and one z. The windows aybz (length 4) and bzca (length 4) work, but abz at the end has length 3, and no shorter window can hold three letters. So the answer is abz.
Example 2
s = "aab"t = "aa"return = "aa"t has two as, so the window must contain both. The first two characters aa do.
Example 3
s = "a"t = "aa"return = ""s has only one a but t needs two, so no window works and the answer is the empty string.
Constraints
1 <= s.length, t.length <= 5 * 10^4sandtconsist of English letters (upper case and lower case).
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Window SubstringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Minimum Window SubstringLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Minimum Covering SubstringByteDance · ONSITE INTERVIEWByteDance interview questions
- Minimum Window SubstringGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Minimum Window SubstringHighspot · ONSITE INTERVIEW
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
- Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
- Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)