Custom Sorting
Filed under Amazon
Free read 2 of 3Identify the exact sorting criteria (frequency, custom alphabet order, etc.).
The question
Perform a custom sort on a string based on a given custom order or frequency rules.
Constraints
- String length up to 10^4
- Characters consist of ASCII values
Examples
- Input
- s = "cba", order = "abc"
- Output
- "abc"
- Why
- Characters of s are sorted according to the custom order string.
Approach
Identify the exact sorting criteria (frequency, custom alphabet order, etc.).
Use a custom comparator function with standard sorting algorithms.
Use a frequency hash map or array to construct the sorted string in linear time based on the target order.
Empty string, characters in string not present in custom order.
Tips
- What they are testing: Tests efficient frequency counting and custom comparator implementation.
- Common mistake: Inefficient sorting algorithms leading to O(N^2 log N) performance instead of O(N).
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.