Make Lexicographically Smallest Array by Swapping Elements
A medium-tier problem at 60% community acceptance, tagged with Array, Union Find, Sorting. Reported in interviews at PhonePe and 2 others.
PhonePe, Atlassian, and IBM have asked this one. You're given an array and a list of pairs that can swap. The goal: rearrange to the lexicographically smallest result. The trap is thinking you can swap any two elements freely. You can't. The trick is figuring out which elements can eventually reach which positions through a chain of allowed swaps. Union Find solves this in one pass. If you hit this live and freeze on the grouping logic, StealthCoder surfaces the solution invisible to the proctor.
Companies that ask "Make Lexicographically Smallest Array by Swapping Elements"
Make Lexicographically Smallest Array by Swapping Elements is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe problem is really about connected components. Two indices are in the same component if they can swap (directly or transitively). Within each component, you can arrange elements in any order. So sort the elements in each component, sort the indices in each component, and zip them back. Most candidates see the swap list and try greedy or simulation, which fails on test cases with transitive relationships. The Union Find insight flips it: build the groups first, then sort within groups. That's the entire algorithm. Sorting is also in the topics because you're sorting both indices and values per component. This is a medium-difficulty problem because the pattern isn't obvious until you see it, but once you do, the code is straightforward.
Pattern tags
You know the problem.
Make sure you actually pass it.
Make Lexicographically Smallest Array by Swapping Elements recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Make Lexicographically Smallest Array by Swapping Elements interview FAQ
Why is Union Find the right tool here?+
Union Find identifies which elements can reach which positions through a chain of swaps. Each swap creates a connection between two indices. Elements in the same connected component can rearrange freely among their positions. Build components once, then sort within each component. It's O(n alpha(n)), not O(n^2).
What's the pitfall most people hit?+
Thinking you can sort the entire array or apply swaps greedily. You can't. Swaps are constrained. The aha moment is realizing you need to identify which indices form groups first, partition the values and indices by group, then sort and recombine. Miss that and you'll TLE or fail cases with transitive swaps.
Is this still asked at FAANG if it's labeled medium?+
Yes. PhonePe and Atlassian have asked it. Medium doesn't mean weak. It means the solution requires one key insight (connected components) but isn't a long multi-step algorithm. That insight is what separates a pass from a fail in the live assessment.
How does Union Find connect to the Array and Sorting topics?+
Union Find is the glue. It groups array indices. Sorting happens twice: first you sort elements within each group, then you sort the indices within each group. Then you place sorted elements back into sorted positions. The Array is the input and output; Sorting is the mechanic.
What if I forget to implement Union Find on the fly?+
You can sketch it in seconds: parent array, find with path compression, union by rank. But if you blank on the structure mid-OA, StealthCoder runs invisibly and gives you a working template. You paste it and move on. That's the hedge.
Want the actual problem statement? View "Make Lexicographically Smallest Array by Swapping Elements" on LeetCode →