MEDIUMasked at 2 companies

Smallest String With Swaps

A medium-tier problem at 59% community acceptance, tagged with Array, Hash Table, String. Reported in interviews at PhonePe and 1 others.

Founder's read

Smallest String With Swaps is a medium-difficulty problem that's been asked at PayPal and PhonePe. You're given a string and pairs of indices you can swap freely. The goal is to rearrange the string to its lexicographically smallest form. The trick isn't greedy swapping. It's realizing that if you can swap indices 0 and 2, and 2 and 5, then 0, 2, and 5 form a connected component. You can sort characters within each component independently. Most candidates miss the graph connectivity angle and try to simulate swaps, which times out. If this problem hits your live assessment and you blank on the Union Find or DFS pattern, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
2
Difficulty
MEDIUM
Acceptance
59%

Companies that ask "Smallest String With Swaps"

If this hits your live OA

Smallest String With Swaps 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 realized the OA tests how well you memorized 200 problems, not how well you code.

Get StealthCoder
What this means

The problem is a graph connectivity problem masquerading as a string puzzle. Each index is a node. Each swap pair is an edge. Find all connected components using Union Find, DFS, or BFS. For each component, collect the characters at those indices and sort them. Reassign the sorted characters back to the sorted indices within the component. The brute force approach (simulate swaps one at a time) fails on large inputs. The correct insight is that within a connected component, you can achieve any permutation of characters, so you pick the lexicographically smallest arrangement. Hash Table and Sorting are part of the solution workflow. Union Find is the cleanest implementation. StealthCoder is your hedge if you see this live and the graph model doesn't click immediately.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Smallest String With Swaps 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 realized the OA tests how well you memorized 200 problems, not how well you code. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Smallest String With Swaps interview FAQ

Do I need Union Find to solve this, or can I use DFS?+

Both work. Union Find is more concise for grouping indices. DFS and BFS also correctly identify connected components. Union Find is slightly faster and cleaner to code in an interview. Pick whichever you're most comfortable implementing without bugs under time pressure.

Is this still asked at FAANG and similar companies?+

It's been reported at PayPal and PhonePe. It's a solid medium-difficulty problem, so it appears in interview rotations at companies that focus on graph and string problems. Not every company asks it, but placement is consistent enough to warrant prep.

What's the acceptance rate and why is it not higher?+

Acceptance sits around 59 percent. Most failures stem from not recognizing the graph structure early. Candidates try greedy or simulation approaches, which either give wrong answers or time out. The insight is the hard part, not the implementation.

How does this relate to other Union Find problems?+

This is a classic application of Union Find to partition a set and then solve sub-problems on each partition. Similar structure to problems involving connected components, friend circles, and account merging. If you've done those, the pattern feels familiar.

What's the gotcha if I try to code this naively?+

Simulating swaps directly or using a greedy approach costs O(n squared) or worse. You also risk incorrect results if you don't account for transitive swap chains. The moment you recognize it as a graph problem, the solution becomes clear and efficient.

Want the actual problem statement? View "Smallest String With Swaps" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.