Data Structures & AlgorithmsHardProduct-company MCQ pattern

n integers, each in the range 0 to n² − 1, must be sorted. Which approach sorts them in O(n) time?

  1. A. Heap sort
  2. B. Radix sort with base n (2 passes of counting sort)
  3. C. Counting sort directly on values 0 to n² − 1
  4. D. Merge sort

Correct answer: B. Radix sort with base n (2 passes of counting sort)

Written in base n each key has 2 digits, and each digit pass is a counting sort over n buckets, so the total is O(2(n + n)) = O(n). Counting sort directly needs an O(n²)-sized count array, and comparison sorts need Ω(n log n).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic