Lexicographically Maximum Final Sequence
Updated
Problem statement
You are given a binary string shipmentData. It contains only '0' and '1'.
First, rearrange the characters of shipmentData in any order you like. Then build a string finalSequence from your rearranged shipmentData like this:
- Start with an empty
finalSequence. - Go through your rearranged string from left to right. For each character
c, addcto the end offinalSequence, then reversefinalSequence.
Your goal is the lexicographically maximum finalSequence. That is the one that would come last in dictionary order: compare two results from the left, and at the first place they differ, the one with the larger character is bigger. Return the rearranged shipmentData string that produces it.
Do not return finalSequence itself. Your answer must be a rearrangement of the original shipmentData: the same characters, possibly in a different order.
Function
rearrangeShipmentData(shipmentData: String) → StringExamples
Example 1
shipmentData = "0011"return = "0101"The rearranged string "0101" builds the final sequence "1100", with both ones at the front. No other rearrangement builds a larger final sequence, so this string is the answer.
Example 2
shipmentData = "10100"return = "00101"After all the steps, the final sequence lists the characters of the rearranged string from positions 5,3,1,2,4, in that order. To make it as large as possible, put the two '1' characters at positions 5 and 3. The final sequence is then "11000", and the rearranged string with ones at those positions is the answer.
Constraints
shipmentData.length >= 1shipmentDatacontains only'0'and'1'.
More Amazon Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026