PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This set of problems evaluates proficiency in string manipulation, priority-queue/heap usage and greedy strategies, and linked-list operations including pointer management and cloning, reflecting core data structure and algorithm competencies.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Solve LeetCode string and list problems

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Question LeetCode 767. Reorganize String LeetCode 23. Merge k Sorted Lists LeetCode 138. Copy List with Random Pointer https://leetcode.com/problems/reorganize-string/description/ https://leetcode.com/problems/merge-k-sorted-lists/description/ https://leetcode.com/problems/copy-list-with-random-pointer/description/

Quick Answer: This set of problems evaluates proficiency in string manipulation, priority-queue/heap usage and greedy strategies, and linked-list operations including pointer management and cloning, reflecting core data structure and algorithm competencies.

Given a string s of lowercase English letters, rearrange its characters so that no two adjacent characters are the same. Among all valid rearrangements, return the lexicographically smallest one. If no such rearrangement exists, return an empty string.

Constraints

  • 1 <= len(s) <= 100000
  • s consists only of lowercase English letters ('a' to 'z')
  • If multiple valid rearrangements exist, return the lexicographically smallest
  • If no valid rearrangement exists, return an empty string

Examples

Input: aaab

Expected Output:

Input: abb

Expected Output: bab

Input: a

Expected Output: a

Input: aab

Expected Output: aba

Input: aaabc

Expected Output: abaca

Input: aaaabc

Expected Output:

Hints

  1. A rearrangement is impossible if the maximum character frequency exceeds ceil(n/2).
  2. Build the answer greedily, one character at a time.
  3. At each step, pick the smallest letter different from the previous character.
  4. Before committing a choice, ensure the remaining multiset is still feasible: the maximum remaining count must be <= ceil(remaining_length/2).
  5. The alphabet size is small (26), enabling simple O(26) scans per position.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Path Length Through a Grid With One Allowed Cell Conversion - Amazon (medium)
  • Circular Drone Hub Delivery Route - Amazon (hard)
  • Leaf Domain Cumulative Scores - Amazon (medium)
  • Kth Largest Perfect Binary Subtree - Amazon (medium)
  • Find Conflicting Events - Amazon (medium)