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?
- A. Heap sort
- B. Radix sort with base n (2 passes of counting sort)
- C. Counting sort directly on values 0 to n² − 1
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many comparisons does top-down merge sort (split in halves, stop comparing when one side runs out) make on [5, 1, 7, 3, 20, 15, 30, 25]?
- 1800 keys are stored in a hash table with 600 slots using separate chaining and simple uniform hashing. About how many key comparisons…
- How many distinct non-empty substrings does the string ABAB have?
- What is the KMP failure (LPS) array for the pattern AABAACAABAA?
- The reversal algorithm rotates an array LEFT by k: reverse the first k elements, reverse the rest, then reverse the whole array. What is…
- Which statement about a doubly linked list compared with a singly linked list is FALSE?