AO
Back

Custom String Sort by Character Order

CodingOnsiteLast reported March 2026Low Frequency

Problem Overview

Two first-hand onsite reports. You get two strings. One, order, gives every character's rank; one candidate likened it to the alien-dictionary problem. Rearrange the characters of the other string, target, by that order. One report says the sort had to run in linear time; that candidate did not get to a linear sort and was rejected.

Part 1 — Sort a string by a custom order

Write custom_sort(order: str, target: str) -> str.

  • order lists characters from first to last, with no repeats.
  • Return the characters of target rearranged so that every character comes before any character ranked after it in order. Repeated characters all stay.
  • Every character of target appears in order (the tests keep to that; ask what to do otherwise).
  • Aim for O(len(order) + len(target)): a comparison sort is O(n log n).

The function name and argument order are our setup: the two reports list the inputs in different orders.

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Comparison-based sort with custom comparatorSimple to implement — build a rank map then sort target using that map as the comparator key. Time complexity is O(n log n), which does not meet the linear time requirement.

What Reports Emphasize

Common mistakes: One candidate did not get the sort to linear time and was rejected within a week, with no detailed feedback.

Interviewer hints: One report says linear sorting time was required.

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Waymo · Coding · Reported 2× across candidate reports
Is this helpful?