Data Structures & AlgorithmsHardAccenture · Infosys OA
Problem: Counting sort can sort n integers in the range [0, k] in O(n + k) time. What is the key limitation that makes it unsuitable for general-purpose sorting?
- A. It cannot be made stable
- B. It is always slower than comparison sorts
- C. It needs k (the value range) to be reasonably small; a huge range makes it memory- and time-inefficient despite being linear in n+k
- D. It cannot sort negative numbers under any circumstances
Correct answer: C. It needs k (the value range) to be reasonably small; a huge range makes it memory- and time-inefficient despite being linear in n+k
Counting sort allocates and scans an auxiliary array of size k (the value range); if k is very large compared to n (e.g. sorting a few floats or huge-range integers), the O(n+k) cost is dominated by k, making it impractical.
More Coding Interview Problems questions
- Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?
- Problem: Finding the median of two sorted arrays of sizes m and n can be done faster than merging them. What is the best known time…
- Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?
- Problem: Generating all permutations of an n-element array via backtracking takes how much time, and why?