AO
Back

Custom String Sort by Character Order

CodingPhone, OnsiteSoftware Engineer, Machine Learning EngineerLast reported March 2026Low Frequency

Problem Overview

Given two input strings: order and target.

Full problem statement

Given two input strings: order and target. The order string defines a custom character ordering (similar to the alien dictionary problem). Sort the characters in target according to the precedence defined by order. The solution is expected to run in linear time O(n) with respect to the length of target.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 2
Trade-off discussion

Can you achieve linear time complexity O(n)?

Probes for: Candidate produces a working solution

What if there are characters in target that don't appear in order? How do you handle them?

Probes for: Candidate explains the approach

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: Failing to achieve linear time complexity for the sort — at least one candidate did not reach a linear solution and was noted as failing specifically on that requirement.

Interviewer hints: The interviewer explicitly required linear sorting time O(n) with respect to the length of target, and this was a hard requirement that determined pass/fail.

Practice

Write your own against 5 test cases, or read the worked solution — approach, complexity, and code that runs.
Waymo · Coding · Reported 2× across candidate reports
Is this helpful?