The Height Checker interview question asks you to compare a given array of heights with the "expected" order. The expected order is simply the heights sorted in non-decreasing order. You need to return the number of indices where the actual height in the input array does not match the expected height in the sorted array.
This "Easy" question is used by Amazon and Meta to test basic array manipulation and sorting concepts. It evaluates whether a candidate understands the simplest way to identify differences between two datasets. While the straightforward solution is sorting, it can also lead to discussions about Counting Sort interview patterns, which can solve the problem in linear time when the range of values is small.
The standard pattern is Sorting and Comparison.
Alternatively, since heights are typically within a small range (e.g., 1 to 100), you can use Counting Sort to achieve time complexity.
Heights: [1, 1, 4, 2, 1, 3]
[1, 1, 1, 2, 3, 4]Always consider the constraints. If the values in an array are constrained to a small range (like 1-100), Counting Sort is a great optimization to mention. It shows you can adapt your approach based on specific data properties to improve time complexity from to .
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| H-Index | Medium | Solve | |
| Minimum Number of Moves to Seat Everyone | Easy | Solve | |
| Array Partition | Easy | Solve | |
| How Many Numbers Are Smaller Than the Current Number | Easy | Solve | |
| Relative Sort Array | Easy | Solve |