Counting Sort is a non-comparison sorting algorithm that works by counting how many times each value appears in an input array. Instead of comparing elements like typical sorting algorithms, it builds a frequency array and reconstructs the sorted result based on those counts. This approach makes Counting Sort extremely efficient when the range of values is limited.
For coding interviews, Counting Sort is important because it introduces the idea of frequency counting and prefix-based reconstruction. Many interview problems rely on this pattern when dealing with integer ranges, character frequencies, or bucket-style grouping. Understanding Counting Sort also helps you recognize when you can replace slower comparison-based approaches with linear-time solutions.
In practice problems, Counting Sort often appears in variations such as:
To master this topic, you should already be comfortable with Array manipulation and general Sorting concepts. The practice questions in this section will help you understand when Counting Sort is the right tool and how to implement it efficiently in coding interviews.
Counting Sort relies heavily on indexing and iterating through arrays to store and reconstruct frequency counts.
Understanding general sorting concepts helps you compare Counting Sort with comparison-based algorithms like quicksort or mergesort.
Prefix sums are used to convert frequency counts into positions when building the final sorted output.
Counting Sort is commonly used as a stable subroutine inside Radix Sort for sorting digits efficiently.
| Status | Title | Video | Leetcode | Solve | Difficulty | Companies | Topics |
|---|---|---|---|---|---|---|---|
| 274. H-Index | Solve | Medium | Alation+4 | ||||
| 561. Array Partition | Solve | Easy | Adobe+2 | ||||
| 912. Sort an Array | Solve | Medium | Amazon+1 | ||||
| 1051. Height Checker | Solve | Easy | Apple+2 | ||||
| 1122. Relative Sort Array | Solve | Easy | Google | ||||
| 1833. Maximum Ice Cream Bars | Solve | Medium | Amazon | ||||
| 2037. Minimum Number of Moves to Seat Everyone | Solve | Easy | Adobe | ||||
| 3088. Make String Anti-palindrome | Solve | Hard | - | ||||
| 3189. Minimum Moves to Get a Peaceful Board | Solve | Medium | - |
Start Easy, progress to Hard.
Frequently appear alongside Counting Sort.
Common questions about Counting Sort.
Yes, Counting Sort can be implemented as a stable sorting algorithm. Stability is important when Counting Sort is used inside algorithms like Radix Sort, where the order of equal elements must be preserved.
Counting Sort is ideal when you are sorting integers or characters with a small and known value range. In such cases, it can achieve linear time complexity and outperform comparison-based sorting algorithms.
The time complexity of Counting Sort is O(n + k), where n is the number of elements and k is the range of input values. This makes it very efficient when k is relatively small compared to n.
Counting Sort is a non-comparison sorting algorithm that counts the frequency of each value in the input and reconstructs the sorted array using those counts. It works best when the range of input values is not significantly larger than the number of elements.
Practicing multiple variations helps you recognize the frequency-counting pattern quickly. Solving around 8–10 well-chosen problems, like the 9 available in this section, is usually enough to build strong intuition.