DSA interview question
A search engine highlights the shortest snippet of a document that contains every character of the query, with multiplicity (a query "AAB" needs two As and a B). Given s and t, return the minimum-length substring of s that contains all of t's characters, or "" if none exists. A sliding window with a "still missing" counter solves it in O(|s| + |t|). Extend right until nothing is missing, then shrink from the left as long as the window still covers t, recording the best window before each shrink breaks coverage.
Example: s = "ADOBECODEBANC", t = "ABC" → "BANC".
string minWindow(const string& s, const string& t); // "" if no window exists
Input (s, t) | Output | Explanation |
|---|---|---|
("ADOBECODEBANC", "ABC") | "BANC" | |
("a", "a") | "a" | |
("a", "aa") | "" | Needs two a's |
("ab", "b") | "b" | |
("aaflslflsldkalskaaa", "aaa") | "aaa" | |
("cabwefgewcwaefgcf", "cae") | "cwae" |
(Confirm: duplicates in t must be matched with multiplicity; characters are ASCII; if several windows tie, return the leftmost.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in