FastPrepRearrange a String So Adjacent Characters Differ
Problems/Rearrange a String So Adjacent Characters Differ

Rearrange a String So Adjacent Characters Differ

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks to rearrange a string so that neighboring characters differ. We require the lexicographically smallest valid output, and a clear result when it is impossible. Our version is about 88% the same as the reported question.

Rearrange all characters of s so no two adjacent characters are equal.

Return the lexicographically smallest valid rearrangement: compare strings from left to right, and at the first difference choose the smaller character. Return the empty string when no valid rearrangement exists. Character comparisons use ASCII order.

Function

rearrangeWithoutAdjacentEquals(s: String) → String

Examples

Example 1

s = "aab"return = "aba"

aba is valid and is the only arrangement without equal neighbors.

Example 2

s = "aaab"return = ""

Three copies of a cannot be separated by one b.

Example 3

s = "aabbcc"return = "abacbc"

This is the lexicographically smallest valid use of all characters.

Constraints

  • 0 <= s.length <= 2000.
  • s contains ASCII letters.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEW
  • Worker Task Assignment Part 1 - Balance WorkloadStripe · ONSITE INTERVIEW
  • Lexicographically Smallest School AssemblyWells Fargo · OA
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEW
  • Rearrange a String So No Neighbors MatchThe D.E. Shaw Group · ONSITE INTERVIEW
  • Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEW
  • Minimum Nonadjacent Flips for an Alternating Binary StringInfosys · ONSITE INTERVIEW
  • Shortest Sequence of Source SubsequencesPinterest · ONSITE INTERVIEW
  • Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEW

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
See Google hiring insights
Saved
class Solution {
    public String rearrangeWithoutAdjacentEquals(String s) {
        // Write your code here.
    }
}
Checking account…