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<sup>th</sup>
person.
Return names
* sorted in descending order by the people's heights*.
Example 1:
names = ["Mary","John","Emma"]
, heights = [180,165,170]
Output: ["Mary","Emma","John"]
Example 2:
names = ["Alice","Bob","Bob"]
, heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
How would you approach solving this problem efficiently? Consider both time and space complexity. Also, discuss any edge cases that might affect your solution.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking all possible arrangements of people according to their heights. Think of it like shuffling a deck of cards and checking if the order is correct. We continue shuffling until we find an order that satisfies the height requirements.
Here's how the algorithm would work step-by-step:
import itertools
def sort_the_people_brute_force(names, heights):
number_of_people = len(names)
# Generate all possible permutations of indices
index_permutations = itertools.permutations(range(number_of_people))
for permutation in index_permutations:
is_sorted = True
# Check if the current permutation sorts heights
for i in range(number_of_people - 1):
if heights[permutation[i]] < heights[permutation[i + 1]]:
is_sorted = False
break
# If the permutation is sorted, construct the result
if is_sorted:
sorted_names = [names[permutation[i]] for i in range(number_of_people)]
return sorted_names
# If no permutation sorts the heights, return an empty list
return []
The problem asks us to sort people based on their heights. The clever trick is to use the heights as a direct link to the people's names, because the heights are unique. This allows us to avoid slow comparisons.
Here's how the algorithm would work step-by-step:
def sort_the_people(names, heights):
height_to_name = {}
# Create a dictionary to map heights to names.
for index in range(len(names)):
height_to_name[heights[index]] = names[index]
sorted_heights = sorted(heights)
sorted_names = []
# Build the result by looking up names using sorted heights.
for height in sorted_heights:
sorted_names.append(height_to_name[height])
sorted_names.reverse()
# Reverse to get names sorted by height descending
return sorted_names
Case | How to Handle |
---|---|
Empty names or heights array | Return an empty array immediately as there's nothing to sort. |
Arrays of unequal length | Throw an IllegalArgumentException (or similar) indicating invalid input, or return an empty array. |
Single element array | Return the input names array directly, as it is already sorted. |
All heights are the same | The sorting algorithm should still function correctly, returning names in the order they appeared in the original array based on stability. |
Heights are in reverse sorted order | Check that the sorting algorithm performs reasonably well (e.g., doesn't degrade to O(n^2) for quicksort). |
Very large number of people (nearing memory limits) | Consider the memory footprint of the sorting algorithm and data structures used (e.g., avoid unnecessary copies). |
Heights containing duplicate values with different corresponding names | The algorithm will sort based on the height values, maintaining the original ordering of names for equal heights if the sort is stable. |
Names containing Unicode characters or special characters | Ensure the sorting algorithm and string comparison handles Unicode characters correctly to avoid unexpected behavior. |