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?

  1. A. It cannot be made stable
  2. B. It is always slower than comparison sorts
  3. 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
  4. 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

Exams that ask this topic