Shortest Watch History Window Containing a Playlist in Order

Last updated: November 29, 2026

Quick Overview

Given a user's YouTube watch history as a string of video codes and a playlist string, return the shortest contiguous window of the history that contains the playlist as a subsequence (in order). Return the leftmost one if tied, or an empty string.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a user's YouTube watch history as a string of video codes and a playlist string, return the shortest contiguous window of the history that contains the playlist as a subsequence (in order). Return the leftmost one if tied, or an empty string.

A Google onsite Hard problem that is easy to confuse with minimum window substring, but order matters here. The interviewer checks a forward scan to find a match end and a backward scan to tighten the start, and whether the candidate can explain the complexity.

What the Interviewer Expects
  • Distinguishes subsequence-in-order from multiset containment
  • Scans forward to find where the playlist is completed
  • Scans backward from that end to find the tightest start
  • Restarts the forward scan from just after the tight start
  • States O(n * m) worst case and compares with a DP solution
Key Topics to Cover
Two Pointers
Subsequence
Forward and Backward Scan
Strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Give the O(n * m) DP formulation.
  • How would you speed this up with next-occurrence tables?
  • What if you need the count of all minimal windows?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: strings hist (length n) and play (length m).
  • Output: the shortest substring of hist that has play as a subsequence; leftmost on ties; "" if none....
High-Level Design

Forward pass: from position i, walk hist matching play greedily until the last playlist character is matched at index end. This gives the earliest possible end for a window starting at or afte...


Submit Your Answer
Markdown supported