Skip to main content

Sort the People - Solution & Explanation

EasyArrayHash TableStringSorting16 min readAsked at: Amazon, Microsoft, Meta +3
Practice this problem

Problem Statement

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

For each index i, names[i] and heights[i] denote the name and height of the ith person.

Return names sorted in descending order by the people's heights.

 

Example 1:

Input: names = ["Mary","John","Emma"], heights = [180,165,170]
Output: ["Mary","Emma","John"]
Explanation: Mary is the tallest, followed by Emma and John.

Example 2:

Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.

 

Constraints:

  • n == names.length == heights.length
  • 1 <= n <= 103
  • 1 <= names[i].length <= 20
  • 1 <= heights[i] <= 105
  • names[i] consists of lower and upper case English letters.
  • All the values of heights are distinct.

Approach Overview

Problem Overview: You receive two arrays: names and heights. Each index represents a person. The task is to return the names sorted in descending order of height while keeping the correct name-height association.

This problem is essentially a sorting task with linked data. The key constraint is preserving the relationship between each name and its height while ordering by height. Most solutions rely on combining or referencing indices so the pairing is never lost during sorting.

Approach 1: Pairing and Sorting (Time: O(n log n), Space: O(n))

Create pairs that keep each person's name tied to their height. For example, combine them as (height, name) tuples or objects. Once paired, sort the list by height in descending order using a standard sorting algorithm. After sorting, iterate through the pairs and extract the names into the result array.

The key insight is that pairing eliminates the risk of losing the relationship between the two arrays during sorting. This approach is straightforward and maps directly to built‑in sort utilities available in most languages. It relies on concepts from array manipulation and sorting. Because the entire list is sorted, the complexity is dominated by the sorting step.

Approach 2: Directly Sorting Indices (Time: O(n log n), Space: O(n))

Instead of pairing values, create an index array containing 0..n-1. Sort this index array based on the values in the heights array, ordering indices so that taller heights appear first. Once sorted, iterate through the index list and pick the corresponding names from the names array.

This approach keeps the original arrays untouched and only reorders indices. The technique is common when working with parallel arrays and avoids allocating pair objects. It still performs a comparison-based sort, so the time complexity remains O(n log n). The idea frequently appears in problems involving array indexing and comparator-based sorting.

Recommended for interviews: Pairing and sorting is the most direct and readable solution, which makes it the expected answer in many interviews. Sorting indices demonstrates deeper control over memory layout and array manipulation, which can be useful when dealing with large datasets or immutable structures. Showing both approaches signals strong understanding of how to maintain relationships between multiple arrays during sorting.

Approach 1: Approach 1: Pairing and Sorting

This approach involves pairing each name with its corresponding height, sorting these pairs by height in descending order, and then extracting the sorted names. This makes use of the association between each name and its corresponding height.

The code creates a list of tuples, each containing a height and the corresponding name. The list is then sorted in descending order based on the height, by using a lambda function as the sorting key. Finally, the sorted names are extracted from the tuples.

Code

Python

Java

C++

C

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to the sorting operation.
Space Complexity: O(n) for storing the list of tuples.

Try this approach in the editor →

Approach 2: Approach 2: Directly Sorting Indices

Rather than pairing names and heights, you can directly sort the indices of the height array. After sorting the indices by descending heights, you can retrieve the names based on this sorted order of heights.

The code sorts indices based on the heights using negative heights to direct the sorting to descending order. After sorting, use these indices to create the sorted list of names.

Code

Python

Java

C++

C

C#

JavaScript

Complexity

Time Complexity: O(n log n) due to sorting the indices.
Space Complexity: O(n) for auxiliary storage of indices.

Try this approach in the editor →

Approach 3: Sorting

According to the problem description, we can create an index array idx of length n, where idx[i]=i. Then we sort each index in idx in descending order according to the corresponding height in heights. Finally, we traverse each index i in the sorted idx and add names[i] to the answer array.

We can also create an array arr of length n, where each element is a tuple (heights[i], i). Then we sort arr in descending order by height. Finally, we traverse each element (heights[i], i) in the sorted arr and add names[i] to the answer array.

The time complexity is O(n times log n), and the space complexity is O(n). Here, n is the length of the arrays names and heights.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 4: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Pairing and Sorting

Time Complexity: O(n log n) due to the sorting operation.
Space Complexity: O(n) for storing the list of tuples.

Approach 2: Directly Sorting Indices

Time Complexity: O(n log n) due to sorting the indices.
Space Complexity: O(n) for auxiliary storage of indices.

Sorting—
Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Pairing and SortingO(n log n)O(n)Most common and readable approach; ideal when using built-in sort with paired values.
Directly Sorting IndicesO(n log n)O(n)Useful when you want to avoid modifying the original arrays and only reorder references.

Video Solution

Sort the People - Leetcode 2418 - Python • NeetCodeIO • 7,289 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Sort the People easy or hard?
Sort the People is classified as an Easy problem. The main idea is recognizing that you must sort while preserving the mapping between names and heights. Once that relationship is maintained, the implementation becomes straightforward.
Sort the People Python/Java solution
In Python, you can combine heights and names using zip and sort by height in descending order. In Java, create pairs or sort indices using a custom comparator referencing the heights array. Both implementations run in O(n log n) time.
How to solve Sort the People in O(n)?
An O(n) solution is generally not possible because the output requires ordering elements by value, which typically requires comparison-based sorting. Standard solutions rely on sorting heights, resulting in O(n log n) time complexity.
What is the best approach for Sort the People?
The most common solution pairs each name with its height and sorts the pairs in descending order of height. This keeps the relationship between the two arrays intact and uses a standard sort operation. The time complexity is O(n log n) and space complexity is O(n).
Is Sort the People asked at Google/Amazon/Meta?
Problems involving sorting paired data and maintaining relationships between arrays appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact question may vary, the pattern of sorting based on a key while preserving associated values is common.
What data structure is used in Sort the People?
The solution typically uses arrays along with either paired tuples or an index array. Sorting with a custom comparator is the main technique, making it a classic array and sorting problem.
What is the time complexity of Sort the People?
The dominant step is sorting the people by height. Comparison-based sorting requires O(n log n) time for n people. Space complexity is typically O(n) if pairs or index arrays are created during the process.

Ready to solve this problem?

Practice Sort the People with our built-in code editor and test cases.

Practice on FleetCode