Bucket Sort with Parallel Workers
Updated
Problem statement
Given an integer array values and a positive integer workers, return a new array containing all the values in nondecreasing numeric order. Retain every duplicate occurrence and leave the supplied array unchanged.
Value buckets
Use workers independent value buckets, numbered from 0 through workers - 1. For a nonempty input, let minimum and maximum be its smallest and largest values, and let span = maximum - minimum + 1.
Assign each value x to bucket floor((x - minimum) * workers / span). Sort each bucket independently, then concatenate the sorted buckets in increasing bucket-number order. Each bucket covers its own range of values, so joining the sorted buckets gives a fully sorted array.
For an empty input, return []. The product in the formula can be large, so use an integer type that will not overflow.
Parallel follow-up
Explain how the bucket sorting can run on workers processors. Discuss distributing values, keeping each bucket private while it is sorted, waiting for all bucket sorts to finish, and assigning disjoint output ranges. Analyze how a heavily skewed bucket limits the benefit of parallel sorting.
Your function can run the buckets one after another. The follow-up asks how to run the same independent bucket work at the same time.
Function
bucketSortWithWorkers(values: int[], workers: int) → int[]Examples
Example 1
values = [9,0,5,9,-2]workers = 3return = [-2,0,5,9,9]The minimum is -2 and the inclusive span is 12. The three buckets receive [0,-2], [5], and [9,9]. Sorting each and concatenating them gives [-2,0,5,9,9].
Example 2
values = []workers = 4return = []An empty input has no minimum or maximum. Its sorted result is an empty array for every valid worker count.
Example 3
values = [1000000000,-1000000000,0]workers = 16return = [-1000000000,0,1000000000]The inclusive span is 2000000001. The values belong to buckets 15, 0, and 7, respectively. Ordered bucket concatenation returns the three numeric values in ascending order.
Constraints
0 <= values.length <= 2000.-10^9 <= values[i] <= 10^9.1 <= workers <= 16.- Duplicate values are allowed.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Longest Uninterrupted Work BlockIntuit · PHONE SCREENIntuit interview questions
- Streamed Scalar Item TripletsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Bus Station Seat AllotmentInMobi · PHONE SCREENInMobi interview questions
- Implement Beam Search DecodingApple · PHONE SCREENApple interview questions
- Wordle Guess LoopRetool · PHONE SCREENRetool interview questions