Reported February 2026
Amazongreedy

Lexicographically Smallest After One Substring Rotation

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reportedly served this one in February 2026, and the first attempt that fails is the obvious one: rotate the whole string and call it done. The task is to pick one substring, rotate it right by one so its last character jumps to the front, and return the smallest string you can get. The hinted pattern is sliding-window, but the real work is a greedy choice of where the window starts and ends. If you blank mid-assessment, StealthCoder is the safety net that runs invisibly and reads the prompt for you.

The problem

You are given a string s. You must choose one non-empty contiguous substring of s and rotate that substring to the right by one position exactly once.
Rotating a substring to the right by one position moves its last character to the front of that substring, while every other character in the substring shifts one position to the right.
Return the lexicographically smallest string that can be obtained after performing the operation.

Function
smallestStringAfterOneRotation(s: String) → String

Examples
Example 1
s = "baca"
return = "abac"
Choose the whole string baca. Rotating it right by one position moves the last character to the front, giving abac, which is the lexicographically smallest result obtainable.
Example 2
s = "cba"
return = "acb"
Choose the whole string "cba". Rotating it right gives "acb", which is the smallest possible result.

Constraints
s is non-empty.
The operation must be performed exactly once. Choosing a substring of length 1 leaves the string unchanged.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Rotating s[l..r] right by one pulls s[r] to position l and shifts the rest right. To get the smallest result, you want a small character moved as far left as possible, in front of a larger one. Find the first index l where some later character is strictly smaller than s[l]. Then pick r as the position of the smallest character after l, and test ties by comparing the resulting strings, since the shifted tail differs. The pitfall is rotating the whole string by default. In example 1, baca gives abac, but only because the 'a' lands in front of 'b'. Leading characters that are already minimal should stay untouched. If the string is non-decreasing, any rotation can only hurt, so choose a length-1 substring and return s unchanged. With n small enough, brute-forcing candidate r values for the chosen l is safe. StealthCoder is the hedge if the tie-break logic slips under pressure in the live OA.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Lexicographically Smallest After One Substring Rotation cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lexicographically Smallest After One Substring Rotation FAQ

What's the trick in this Amazon rotation problem?+

Greedy. Rotating right pulls the last character of your window to the front. So find the first position where a smaller character exists later, then bring the best such character forward. Everything before that position is already as small as it can be, so leave it alone.

Why does rotating the whole string fail?+

It only works when the last character happens to be the smallest and the prefix doesn't matter. In general it can make the front worse. Example 2 works with cba because the 'a' at the end is the smallest, but that's a coincidence, not a rule.

What if the string is already sorted?+

Then no rotation helps. The operation must happen exactly once, but a length-1 substring leaves the string unchanged. So return s as is. Handle this case explicitly instead of letting your loop produce something worse.

How do I handle ties when the smallest character repeats?+

Several positions may hold the same smallest character after l. Each choice shifts a different tail, so the results differ. Build each candidate string and keep the minimum. It's cheap for modest input sizes and removes the guesswork.

How do I prepare for this in 48 hours?+

Practice greedy string problems where you scan for the first violation of sorted order, then fix it with the best available character. Write the brute force first, compare it against your greedy on small random strings, and test edge cases like single characters and sorted input.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it