EASYasked at 1 company

Relative Sort Array

A easy-tier problem at 75% community acceptance, tagged with Array, Hash Table, Sorting. Reported in interviews at DE Shaw and 0 others.

Founder's read

Relative Sort Array is a straightforward sorting problem that DE Shaw has asked. You're given two arrays: one to sort, one that defines the order. The catch is simpler than you think, but the naive approach wastes time and memory. An acceptance rate above 74% means most candidates get it, but the gap between a sloppy solution and an optimal one is where you either look sharp or waste cycles during the live assessment. If you hit this in your OA and freeze on the sorting strategy, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
EASY
Acceptance
75%

Companies that ask "Relative Sort Array"

If this hits your live OA

Relative Sort Array is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The problem wants you to sort the first array according to the relative order defined in the second array, then append remaining elements in sorted order. The trap is reaching for a custom comparator when a counting sort approach is cleaner and faster. You need to map each element to its order position, handle elements not in the second array separately, and reconstruct. Hash Table lets you build that position map in one pass. Counting Sort is the underlying insight: you're not doing a general comparison sort, you're bucketizing by a known order and scanning left to right. During a live OA, the temptation to overengineer with a comparator can burn minutes. StealthCoder is your hedge if the pattern doesn't click immediately and you need a working solution fast.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Relative Sort Array recycles across companies for a reason. It's easy-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Relative Sort Array interview FAQ

Is this problem actually easy or is the acceptance rate misleading?+

The 74% acceptance rate is accurate. Most candidates solve it correctly because the problem statement is clear and the brute-force comparator approach works. The gap isn't in correctness, it's in efficiency. You'll pass the OA either way, but optimal solutions stand out during code review or follow-up rounds.

What's the main trick to avoid TLE?+

Avoid sorting the entire first array with a custom comparator tied to the second array. Instead, use a Hash Table to map each element in the second array to its index, then iterate the second array and collect matching elements, then append the rest sorted. This is closer to counting than comparison sorting.

How does Counting Sort fit into this?+

Counting Sort works because the second array defines a finite, ordered set of buckets. You iterate the second array and pull matching elements from the first, which is bucket-filling. Elements not in the second array you sort normally at the end. It avoids O(n log n) comparison sorts for the majority of the work.

Do I need to handle edge cases for duplicates?+

Yes. If an element appears multiple times in the first array and exists in the second array's order, all copies must appear in that position in the relative sort. Use a Hash Table to count occurrences, not just track presence. This is why frequency counting matters here.

Will DE Shaw ask follow-ups if I solve it too fast?+

Possibly. If you finish in two minutes, have a clean explanation of why counting-based approach beats comparison sort. Be ready to discuss space-time tradeoffs and whether you'd use it for large, unsorted second arrays. Don't undersell the solution just because the problem is easy.

Want the actual problem statement? View "Relative Sort Array" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.