FastPrepReorganize a String
Problems/Reorganize a String

Reorganize a String

Amazon logoAmazon● MediumINTERNNEW GRADFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon onsite interview for intern, new grad, and full-time roles. The report accepts any valid arrangement. Our version adds a fixed tie rule. It is about 94% the same as the reported question.

Special thanks: Thanks so much to the friend who shared the last seen date for this question! Wishing them all the best!

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) → String

Examples

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^5
  • s contains 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

Interview reports that mention this question

  • Amazon New Grad SDE Onsite Interview Questions: 17 Experiences

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
See Amazon hiring insights
Saved
class Solution {
    public String reorganizeString(String s) {
        // Write your solution here.
    }
}
Checking account…