Smallest Tag Sequence Keeping Exactly One Copy of Each Tag
Last updated: November 29, 2026
Quick Overview
A string of single-letter labels describes tags applied to a YouTube video in order. Remove duplicates so each distinct letter appears exactly once, preserving relative order, and return the lexicographically smallest such result.
0
0
0 solved
A string of single-letter labels describes tags applied to a YouTube video in order. Remove duplicates so each distinct letter appears exactly once, preserving relative order, and return the lexicographically smallest such result.
Google onsite rounds like this because the monotonic-stack greedy needs two conditions to be right: that a letter can be dropped only if it appears again later. The interviewer checks that you track last occurrences and skip letters already in the stack.
What the Interviewer Expects
- Precompute the last index of every letter
- Use a stack that stays increasing when safe
- Pop a larger letter only if it appears later
- Skip letters already in the result
- O(n) time with O(alphabet) extra space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the largest such subsequence instead.
- Each distinct letter must appear exactly twice in the result.
- The alphabet is all of Unicode. Does the complexity change?
- Prove that the result contains every distinct letter.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a lowercase string s. Output: the lexicographically smallest subsequence containing every distinct letter of s exactly once....
High-Level Design
Scan left to right building the answer in a stack. For each letter c not already in the stack: while the top is greater than c and the top letter occurs again later in s, pop it, because we can ...