Smallest Shelf Label String After Allowed Bin Swaps
Last updated: November 29, 2026
Quick Overview
A shelf holds item labels as a string, and a robot can swap the labels at certain pairs of positions any number of times. Return the lexicographically smallest string achievable.
Amazon
November 29, 20260
0
0 solved
A shelf holds item labels as a string, and a robot can swap the labels at certain pairs of positions any number of times. Return the lexicographically smallest string achievable.
Amazon Robotics rearranges inventory using a limited set of allowed moves. The online assessment question tests whether you realize repeated swaps let any permutation happen within a connected group of positions, which reduces the problem to union find plus sorting.
What the Interviewer Expects
- Observe that swaps generate every permutation inside a connected component
- Union positions with a DSU
- Sort characters within each component and place them in ascending index order
- O(n log n + p alpha(n)) time
- Avoid simulating swaps
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
- Why do transpositions along a connected graph generate all permutations of its nodes?
- What if each swap can be used at most once?
- How would you find the minimum number of swaps to reach the result?
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: string s of length n and list swaps of index pairs, each usable unlimited times. Output: smallest reachable string....
High-Level Design
If positions i and j are connected through a chain of allowed swaps, we can move any character to any position in that chain: adjacent transpositions along a path can produce every permutation of its ...