Smallest Dictionary-Order Bin Code Using Each Zone Letter Once
Last updated: November 29, 2026
Quick Overview
A bin code is built from a string of zone letters, and the warehouse wants a code that keeps the letters' relative order, contains every distinct letter exactly once, and is the smallest in dictionary order. Return that code.
Amazon
November 29, 20260
0
0 solved
A bin code is built from a string of zone letters, and the warehouse wants a code that keeps the letters' relative order, contains every distinct letter exactly once, and is the smallest in dictionary order. Return that code.
An Amazon onsite question where a greedy choice depends on future availability. The interviewer expects a monotonic stack with a last-occurrence map and an in-stack set, and wants the reason a letter can be popped only if it appears again later.
What the Interviewer Expects
- Records the last index of each letter
- Skips letters already in the result
- Pops larger letters that appear again later
- Tracks membership with a set
- Explains why the result is lexicographically minimal
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 smallest subsequence of length k with at most m copies of each letter.
- What if some letters are mandatory and others optional?
- Generalize to Unicode input.
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: lowercase string s, length up to 10^4. Output: the lexicographically smallest subsequence containing every distinct letter of s exactly once....
High-Level Design
Scan left to right building the answer on a stack. Skip a letter that is already on the stack, since keeping its earlier copy is never worse (the stack is kept as small as possible in front). Otherwis...