Reported January 2026
Amazongreedy

Lexicographically Maximum Final Sequence

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

Trying every rearrangement of shipmentData is dead on arrival, since the length has no upper bound and permutations explode fast. Amazon reported this one in January 2026, and it's a greedy index-mapping problem in disguise. The scary part is the append-then-reverse operation, which looks like it needs simulation. It doesn't. Once you see where each character lands in the final string, the answer is a linear pass. If you've got the OA in a day or two, learn the landing order below. And if your mind goes blank mid-assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the mapping.

The problem

You are given a binary string shipmentData consisting only of '0' and '1'.
A final string is built from a chosen ordering of shipmentData as follows:
Start with an empty string finalSequence.
For each character c in the chosen ordering from left to right, append c to finalSequence, then reverse finalSequence.
You may rearrange the characters of shipmentData arbitrarily before applying the operation. Return the rearranged shipmentData string that should be fed into the operation to produce the lexicographically maximum possible finalSequence.
Do not return finalSequence itself. The answer must be a rearrangement of the original shipmentData.

Function
rearrangeShipmentData(shipmentData: String) → String

Examples
Example 1
shipmentData = "0011"
return = "0101"
The returned rearranged string is "0101". If this string is fed into the operation, the resulting final sequence is "1100", which is lexicographically maximum among all rearrangements.
Example 2
shipmentData = "10100"
return = "00101"
The final sequence reads positions 5,3,1,2,4 from the rearranged string. Placing the two '1' characters at positions 5 and 3 produces final sequence "11000".

Constraints
shipmentData.length >= 1
shipmentData contains only '0' and '1'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: append then reverse, repeated, puts the last input character first in the final string, then the characters at positions n-2, n-4 and so on going down, then the remaining positions in ascending order. For n=5 that's positions 5,3,1,2,4, which matches the example. To maximize lexicographically, you want every '1' as early as possible in the final string. So count the ones, walk that landing order, and assign '1' to the first k slots and '0' to the rest. Build the answer array by index, then join it. That's O(n) time. The common pitfall is returning the finalSequence instead of the rearranged input, or getting the parity order wrong for even versus odd n. Test on "0011" giving "0101". StealthCoder is your hedge in the live OA if the index order slips on you.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Lexicographically Maximum Final Sequence 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lexicographically Maximum Final Sequence FAQ

What's the trick in Lexicographically Maximum Final Sequence?+

Don't simulate. Work out where each input position ends up in the final string: last position first, then every second position going backward, then the leftover positions ascending. Put the '1' characters in the earliest final slots, zeros after, and map that back to input indices.

How hard is this Amazon OA question really?+

Medium on paper, easy once you see the mapping. The hard part is noticing that append-then-reverse is just a fixed permutation of positions. After that it's counting ones and filling an array. Most candidates lose time trying to brute force or simulate string reversals, which is quadratic.

What's the time complexity I should aim for?+

Linear, O(n) time and O(n) space. Count the ones, build the position order with two loops, and fill a character array. Simulating each reversal costs O(n) per step, so O(n^2) overall, and that's the approach to avoid given no stated length cap.

Do I return the final sequence or the rearranged string?+

The rearranged shipmentData, not the final sequence. Example 1 returns "0101", even though the final sequence it produces is "1100". Mixing these up is the easiest way to fail every test case after your logic is already correct.

How do I prepare for this in 48 hours?+

Work the two examples by hand. Write the landing order for n=4 and n=5, confirm it matches 4,2,1,3 and 5,3,1,2,4, then code the fill. Also try odd and even lengths, all zeros, and all ones. That covers nearly every edge case this problem has.

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