Reorganize a String
Updated
Problem statement
Rearrange a lowercase string so that no two adjacent characters are equal. If no such arrangement exists, return the empty string.
To make the judged result deterministic, construct the answer with this rule: at each position, select the eligible character with the greatest remaining frequency. The previously placed character is not eligible. If several eligible characters have the same frequency, select the lexicographically smallest one.
Function
reorganizeString(s: String) → StringExamples
Example 1
s = "aab"return = "aba"The highest-frequency eligible character is chosen first, producing a valid arrangement.
Example 2
s = "aaab"return = ""Three copies of one character cannot be separated by the single remaining character.
Example 3
s = "aabbcc"return = "abcabc"Frequency ties are resolved in lexicographic order while the previous character remains temporarily ineligible.
Constraints
1 <= s.length <= 10^5scontains only lowercase English letters.
Source note: Author-written prompt excerpt retained for source fidelity.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Worker Task Assignment Part 1 - Balance WorkloadStripe · ONSITE INTERVIEWStripe interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
- Risk-Priority Detection StoreCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEWFlexport interview questions
- Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Top K Keys from JSON RecordsCapital One · ONSITE INTERVIEWCapital One interview questions