Magicsheet logo

Sort the People

Easy
86.1%
Updated 6/1/2025

Sort the People

What is this problem about?

"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 ii-th name corresponds to the ii-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.

Why is this asked in interviews?

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.

Algorithmic pattern used

The algorithmic pattern used is Zipping and Sorting.

  1. Pair: Create pairs of (height, name) for each person.
  2. Sort: Sort these pairs based on the height in descending order.
  3. Extract: Create a new list containing only the names from the sorted pairs. Alternatively, you can use a Hash Map to store height -> name and then sort the heights array, finally mapping the sorted heights back to their names.

Example explanation

Names: ["Alice", "Bob", "Charlie"], Heights: [155, 185, 150]

  1. Pairs: [(155, "Alice"), (185, "Bob"), (150, "Charlie")]
  2. Sort Descending by height: [(185, "Bob"), (155, "Alice"), (150, "Charlie")]
  3. Extract names: ["Bob", "Alice", "Charlie"]. Result: ["Bob", "Alice", "Charlie"].

Common mistakes candidates make

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.

Interview preparation tip

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.

Similar Questions