EASYasked at 1 company

Lexicographically Smallest String After a Swap

A easy-tier problem at 54% community acceptance, tagged with String, Greedy. Reported in interviews at J.P. Morgan and 0 others.

Founder's read

You've got a string and exactly one swap allowed. The ask: make it lexicographically smallest. Sounds simple until you realize the trap: swapping the first character with anything barely improves the string, but swapping positions deeper in can crater your score. J.P. Morgan has asked this one. It's marked Easy, but the acceptance rate sits at 54%, which means half the candidates either over-complicate the logic or misread the constraints. If this hits your assessment and you freeze on the greedy strategy, StealthCoder will surface the working approach in seconds, invisible to the proctor.

Companies asking
1
Difficulty
EASY
Acceptance
54%

Companies that ask "Lexicographically Smallest String After a Swap"

If this hits your live OA

Lexicographically Smallest String After a Swap 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 StealthCoder
What this means

The pattern is greedy with a scan twist. To get the lexicographically smallest result, you need to find the earliest position where a swap produces improvement, then find the smallest character to its right and swap them. The common mistake: trying every possible swap and picking the best result. That's O(n squared) and unnecessary. The efficient path walks through the string once, identifies the first character that's larger than something to its right, then scans the rest for the smallest character that's actually smaller than it. Tie-breaking matters: if multiple characters match that smallest value, pick the rightmost one (it minimizes disruption). String and Greedy are the topic tags here, and both are required to nail the solution cleanly. This problem feels trivial at first read, then reveals itself when you test on real input. If you haven't drilled the greedy scan pattern, StealthCoder hedges that gap during the live OA.

Pattern tags

The honest play

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

Lexicographically Smallest String After a Swap recycles across companies for a reason. It's easy-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.

Lexicographically Smallest String After a Swap interview FAQ

Why is the acceptance rate only 54% if this is marked Easy?+

Most candidates either brute-force all swaps or mishandle the tie-breaking rule. The greedy scan is simple in words but requires careful pointer logic in code. Off-by-one errors and incorrect tie-break logic sink roughly half the submissions.

What's the actual trick to this problem?+

Scan left to right. Stop at the first character that has something smaller to its right. Find the smallest (and rightmost, if tied) character after it, swap once, return. No nested loops, no sorting. One pass solves it.

Is this still asked at J.P. Morgan?+

It's been reported by at least one source at J.P. Morgan. Whether it's active in their current rotation is unknown, but it's relevant enough to drill, especially if you're prepping for their string-heavy screening round.

How does Greedy relate to finding the lexicographically smallest result?+

Greedy means commit to the first valid improvement and don't second-guess. Here, the first character that can be improved with a smaller swap is your target. Greedily make that swap and you're done; no backtracking needed.

What happens if I swap the first character with everything else?+

You'll often get a larger or unchanged string. The first character is rarely the problem. The greedy scan finds the earliest position that actually benefits from a swap, which is rarely position zero. Testing confirms this fast.

Want the actual problem statement? View "Lexicographically Smallest String After a Swap" 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.