Sort the People - Problem

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 i-th person.

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

Input & Output

Example 1 — Basic Sorting
$ Input: names = ["Mary","John","Emma"], heights = [180,165,170]
Output: ["Mary","Emma","John"]
💡 Note: Mary is tallest (180), Emma is second (170), John is shortest (165). Sorted in descending order by height.
Example 2 — Different Names
$ Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
💡 Note: Bob (185) is tallest, Alice (155) is middle, Bob (150) is shortest. Names can repeat but heights are distinct.
Example 3 — Two People
$ Input: names = ["IEO","Sgizfdfrims"], heights = [165,170]
Output: ["Sgizfdfrims","IEO"]
💡 Note: Sgizfdfrims (170) is taller than IEO (165), so comes first in descending order.

Constraints

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

Visualization

Tap to expand
Sort the People: Input → Processing → OutputInput DataMaryJohnEmmaNames:180165170Heights:Sort by Height (Descending)180 > 170 > 165Sorted OutputMaryEmmaJohnHeight: 180Height: 170Height: 165Result: ["Mary", "Emma", "John"]
Understanding the Visualization
1
Input
Names array and heights array with corresponding indices
2
Process
Sort by heights in descending order while maintaining name-height association
3
Output
Names array sorted by descending height order
Key Takeaway
🎯 Key Insight: Pair data elements together before sorting to maintain relationships between corresponding array elements
Asked in
Amazon 15 Microsoft 12 Google 8
25.0K Views
Medium Frequency
~5 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen