"Sort the People" is a straightforward "Easy" difficulty problem. You are given two arrays: names (an array of strings) and heights (an array of distinct integers). The -th name corresponds to the -th height. Your task is to return the names sorted by their heights in descending order.
Because the heights are distinct, you don't have to worry about ties. This problem is essentially about sorting one array based on the values in another array while keeping the relationship between them intact.
Companies like Meta, Amazon, and Google use this as a quick warm-up to check if a candidate can handle simple data associations. It tests whether you can use a Hash Table to map heights to names or if you can create custom objects/pairs to sort. It’s a test of basic programming syntax and familiarity with sorting utilities in your chosen language.
The algorithmic pattern used is Zipping and Sorting.
(height, name) for each person.height -> name and then sort the heights array, finally mapping the sorted heights back to their names.Names: ["Alice", "Bob", "Charlie"], Heights: [155, 185, 150]
[(155, "Alice"), (185, "Bob"), (150, "Charlie")][(185, "Bob"), (155, "Alice"), (150, "Charlie")]One common mistake is sorting the names and heights arrays independently, which completely destroys the relationship between the people and their heights. Another error is sorting in ascending order when the problem specifically asks for descending order. Some candidates also overcomplicate the problem by using complex sorting algorithms when a simple built-in sort on pairs is sufficient.
For the "Sort the People coding problem," focus on how to "pair" data. In Python, zip(heights, names) is perfect. In Java, you might use a custom Person class or a TreeMap. Showing that you can choose the most concise and readable way to associate and sort data is a great way to demonstrate your coding style.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Resultant Array After Removing Anagrams | Easy | Solve | |
| Invalid Transactions | Medium | Solve | |
| Find And Replace in String | Medium | Solve | |
| Groups of Special-Equivalent Strings | Medium | Solve | |
| Sort Features by Popularity | Medium | Solve |