Data Structures & AlgorithmsModerateTCS Digital · Infosys SP
You need to sort 10⁷ integers, each in the range 0 to 999. Which approach is asymptotically fastest?
- A. Quick sort, average O(n log n)
- B. Heap sort, O(n log n)
- C. Counting sort, O(n + k)
- D. Merge sort, O(n log n)
Correct answer: C. Counting sort, O(n + k)
With only k = 1000 possible values, counting sort runs in O(n + k) = O(n), beating every comparison sort's Ω(n log n) lower bound.
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
- Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed? python a, k = [8, 7, 7, 10, 5, 11…
- Why is a tombstone (a 'deleted' marker) used instead of emptying a slot when deleting from an open-addressing hash table?
- Which statement about using a hash set versus sorting to detect duplicates in an array of n integers is correct?
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- How many contiguous subarrays of [4, 3, 1, 7, 5, 7, 4, 3, 2, 1] contain exactly 2 odd numbers? (Use exactly(k) = atMost(k) − atMost(k − 1).)
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…