Relative Sort (Google Singapore)
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Google Singapore OA in your inbox and Relative Sort is live this month. This isn't a hard problem, but it's easy to overcomplicate. The pattern is simple: you have two arrays, and you need to sort one array based on the relative order defined by the other. Google's using this to filter for clean implementation and attention to edge cases. StealthCoder will catch you if you blank on the sorting hook mid-assessment.
Pattern and pitfall
The trick here is recognizing that you're not sorting by raw values. You're sorting by position in a reference array. Build a map or use index lookup to assign a priority to each element in the first array, then sort based on that priority. Elements not in the reference array typically go at the end in ascending order. The common pitfall is trying to use a comparator without thinking through what you're actually comparing. Time complexity is O(n log n) where n is the size of the first array. StealthCoder will give you the pattern instantly if you stare at the problem blank, so you can code with confidence even under pressure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Relative Sort (Google Singapore) cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as relative sort array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Relative Sort (Google Singapore) FAQ
What if an element in the first array doesn't exist in the reference array?+
Handle it explicitly. Most solutions put those elements at the end, sorted among themselves in ascending order. Check the problem statement for the exact requirement, but that's the standard pattern Google uses.
Do I need a custom comparator?+
Yes, almost always. You're comparing based on position in the reference array, not the values themselves. Use a map to store index or rank, then pass that to your sort function.
Is this really sorting difficulty?+
It's sorting on the surface, but the hard part is logic clarity, not algorithms. If you can implement a comparator cleanly, you're golden. The OA is testing your ability to read a constraint and code it.
What's the space complexity concern?+
Building a map for the reference array is O(m) where m is the size of the reference array. If space is tight, check if sorting in-place is required. Usually it's not a bottleneck.
How do I prepare for this in 24 hours?+
Understand the problem statement cold. Write out the comparator logic on paper first. Practice one custom sort on your language of choice. Don't over-prepare. This tests reading comprehension more than algorithm knowledge.