Order Queries by Shortest Processing Time
Reported by candidates from Scale AI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure this one hinges on is just a sorted list of (duration, index) pairs. Scale AI reported this OA in April 2026, and it's dressed up with intervals and workers, but it's the classic shortest-job-first problem. Each interval [start,end] collapses to a single number, end minus start. Sort by that number, break ties by original index, and you're done. If your head goes blank under the timer, StealthCoder can sit invisibly on your screen as a safety net and hand you the approach. You probably won't need it.
The problem
Given queryIntervals, return the original query indices in an order that minimizes the sum of query completion times. For this exercise, assume an interval [start,end] describes a query whose processing duration is end-start. The endpoints do not constrain its start time. All queries are available at processing time 0, and one worker processes one whole query at a time without preemption or idle gaps. The completion time of a query is the total duration of that query and every query before it in the chosen order. If multiple optimal orders differ only in queries with equal durations, place their original indices in ascending order. Return every zero-based index exactly once. An empty input returns an empty array. The interval data has already been obtained from the supplied input interface; no model or network call is required. Function queryProcessingOrder(queryIntervals: int[][]) → int[] Examples Example 1 queryIntervals = [[0,3],[10,11],[2,4]] return = [1,2,0] The durations are 3, 1, and 2. The returned order completes queries at times 1, 3, and 6, with total completion time 10. Example 2 queryIntervals = [[5,7],[0,2],[9,11]] return = [0,1,2] All durations are 2, so every order has the same objective. The required ascending-index tie rule selects 0, 1, 2. Example 3 queryIntervals = [] return = [] There are no queries to schedule, so the order is empty. Constraints For this exercise, assume 0 <= queryIntervals.length <= 100000. Every row contains two integers with 0 <= start < end <= 1000000000. Rows may be unsorted, overlapping, nested, or identical; every row is a separate query.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is an exchange argument. Completion time of a query is the sum of every duration before it plus its own, so a short query placed early gets counted in fewer later completion times. That means ascending duration minimizes the total. Compute duration as end minus start, ignore where the interval sits, then sort indices by (duration, index). The tie rule is the only real catch. If you use an unstable sort on durations alone, equal durations can come back in the wrong index order. Sort on the pair, or use a stable sort over indices that start ascending. Also handle the empty input and return an empty array. With up to 100000 rows, O(n log n) is fine. Don't sort the interval rows themselves and lose the original indices. Carry the index with each duration. In the live OA, StealthCoder is the hedge if you freeze on the tie-break detail.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Order Queries by Shortest Processing Time 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Scale AI's OA.
Scale AI reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Order Queries by Shortest Processing Time FAQ
What's the trick in the Scale AI query ordering problem?+
Shortest processing time first. Turn each interval into a duration with end minus start, then sort indices by duration ascending. The start and end positions don't matter, only the difference. Overlaps, nesting and identical rows are all distractions.
How do I handle ties between equal durations?+
Sort by the pair (duration, original index). That guarantees equal durations come out in ascending index order, which the problem requires. Example 2 shows it: all durations are 2, so the answer is [0,1,2].
How hard is this really?+
Easy once you spot it. It's a sorting problem wrapped in scheduling language. The risk is overthinking the intervals or losing indices when you sort. The code is a few lines in most languages.
What's the time complexity and does it fit the constraints?+
O(n log n) for the sort and O(n) extra space for the index and duration pairs. With n up to 100000 that's comfortable. Durations go up to 1000000000, so if you also compute the total, use a 64-bit integer.
How do I prepare for this in 48 hours?+
Practice the shortest job first argument and writing a sort with a custom comparator or key tuple. Test your code on the three examples, including the empty array. Then check that you return indices, not durations or intervals.