PPractick

DSA interview question

Minimum Window Subsequence

Hard · Algorithms · Topics: sliding window, dp, string

The problem

A log-forensics tool looks for the shortest contiguous stretch of a log in which a given sequence of events T happens in order, with other events possibly in between. That's a window of S that contains T as a subsequence. Return the shortest such window, the leftmost one if several tie, or "" if none exists. A forward scan finds the earliest end where T is completed. A backward scan from that end, matching T in reverse, finds the latest possible start, which gives the tightest window ending there. Then search again starting just after that start.

Example: S = "abcdebdde", T = "bde" → "bcde".

Function signature

string minWindowSubsequence(const string& S, const string& T);  // T non-empty; "" if none

Test cases

Input (S, T)OutputExplanation
("abcdebdde", "bde")"bcde""bdde" is also length 4 but starts later
("abdbca", "ba")"bca"
("aaa", "aa")"aa"Leftmost of the ties
("xyz", "zy")""Order matters
("abc", "abc")"abc"

(Confirm: T must appear as a subsequence (in order, gaps allowed) inside a contiguous window of S; ties go to the leftmost window.)

How interviewers phrase it

Clarifying questions to ask

  1. Subsequence (order matters, gaps allowed), not just the multiset of characters? (Yes, which is why the minimum-window-substring technique doesn't apply.)
  2. How are ties broken? (Leftmost.)
  3. What are the sizes? (|S| up to 2·10⁴, |T| up to 100.)

Follow-ups to expect

  1. "Why the backward scan?"
  2. "Why restart at s + 1?"
  3. "What's the complexity?"
  4. "DP alternative?"
  5. "How is this different from Minimum Window Substring?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in