Minimum Window Substring

Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live SambaNova Systems OA. Under 2s to a working solution.
Founder's read

The SambaNova Systems OA, reported in June 2022, comes down to one data structure: a frequency map of what t needs, kept live while a window slides across s. That's Minimum Window Substring, a sliding-window problem with a counting twist. You're probably taking it in the next day or two, so here's the shape of it. Find the shortest slice of s that covers every character of t, duplicates included. Return it, or an empty string if nothing fits. With strings up to 10^5 long, brute force dies fast. If your mind goes blank mid-assessment, StealthCoder is the invisible backup that reads the problem and hands you a working solution.

The problem

Given strings s and t, find the shortest contiguous part of s that contains every character of t with its required multiplicity. Return that part of s. If no part qualifies, return an empty string.
For these test cases, a shortest qualifying window, when one exists, is unique. Treat uppercase and lowercase letters as different characters.

Function
minWindow(s: String, t: String) → String

Examples
Example 1
s = "ADOBECODEBANC"
t = "ABC"
return = "BANC"
The final four characters contain each required letter; no shorter window does.
Example 2
s = "a"
t = "a"
return = "a"
The only character supplies the target.
Example 3
s = "a"
t = "aa"
return = ""
There is only one copy of a, so no valid window exists.

Constraints
1 ≤ s.length, t.length ≤ 10^5.
Both strings contain only uppercase and lowercase English letters.
The total input fits in memory.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two pointers plus a count map. Build need[c] from t. Track a single integer, missing, set to t.length. Move the right pointer, and when s[right] is still needed (need[c] > 0), decrement missing. Always decrement need[c], so extras go negative. When missing hits 0, the window is valid. Now shrink from the left while need[s[left]] < 0, since those characters are surplus. Record the window if it's the shortest, then drop one required character and keep going. The common pitfall is ignoring multiplicity and checking only that each letter appears once. Example 3 catches that with t = "aa". Another is rebuilding or scanning the map every step, which turns O(n) into O(n * 52). Case matters, so use a 128-slot array or a map. If you freeze on the shrink logic, StealthCoder is the safety net during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Minimum Window Substring cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum window substring. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Window Substring FAQ

How hard is Minimum Window Substring really?+

It's a LeetCode hard, but the pattern is learnable. The difficulty is keeping the validity check cheap while shrinking correctly. Once you've seen the missing counter approach, it's about 20 lines. Most failures come from off-by-one errors in the shrink loop, not from the idea.

What's the trick to getting O(n) time?+

Never rescan the map. Keep a single missing counter that tracks how many required characters are still uncovered. Each character enters the window once and leaves once, so both pointers move at most n steps. That gives O(n + m) overall.

How do I handle duplicate characters in t?+

Store counts, not a set. need['a'] = 2 means you need two a's. Decrement the count for every character you add, and only reduce missing when the count was positive before the decrement. Example 3, where t is "aa" and s is "a", tests exactly this.

Does case sensitivity matter in this problem?+

Yes. Uppercase and lowercase letters are different characters, so 'a' and 'A' need separate slots. Use an array of size 128 indexed by char code, or a hash map. Don't lowercase the input or you'll return wrong windows.

How do I prepare for this in 48 hours?+

Write the solution from scratch twice on paper or in an editor. Test the three given examples plus an empty-result case and a case where the window is the whole string. Know your shrink condition cold. Then skim similar sliding-window problems so the pattern feels familiar.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with SambaNova Systems.

OA at SambaNova Systems?
Invisible during screen share
Get it