The How Many Numbers Are Smaller Than the Current Number interview question asks you to take an array nums and, for each element nums[i], determine how many other elements in the array are strictly smaller than it. You need to return an array of these counts.
This is a popular screening question at Google, Amazon, and Meta. It evaluates your ability to optimize a brute-force solution. While a nested loop works, interviewers are looking for an or approach. It tests your knowledge of Hash Table interview patterns and sorting techniques to map values to their rank in a sorted set.
There are two main ways to optimize this:
nums = [8, 1, 2, 2, 3]
[1, 2, 2, 3, 8].{1: 0}.{1: 0, 2: 1}.{1: 0, 2: 1, 3: 3}.{1: 0, 2: 1, 3: 3, 8: 4}.[4, 0, 1, 1, 3].Always check the value constraints. If numbers are between 0 and 100, Counting Sort is almost always the intended optimal solution. If the range is large, the sorting and mapping approach is more robust.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Relative Sort Array | Easy | Solve | |
| Rank Transform of an Array | Easy | Solve | |
| Height Checker | Easy | Solve | |
| Make Two Arrays Equal by Reversing Subarrays | Easy | Solve | |
| Sort Array by Increasing Frequency | Easy | Solve |