You are given an array of strings features where each string is a single word. You are also given an array responses, where each response[i] is a string that consists of space-separated words.
You need to sort the features array in descending order based on their popularity. The popularity of a feature is the number of responses that contain the feature. If two features have the same popularity, order them in ascending lexicographical order.
Return the sorted array of features.
Example 1:
Input: features = ["cool","new","awesome"], responses = ["cool product","awesome word","new idea"]
Output: ["cool","awesome","new"]
Explanation: The feature "cool" appears once in the responses, "new" appears once, and "awesome" appears once. Since they all have the same popularity, order them in ascending lexicographical order.
Example 2:
Input: features = ["practical","solution","algorithm"], responses = ["efficient solution","solution","obvious solution"]
Output: ["solution","practical","algorithm"]
Explanation: The feature "solution" appears three times in the responses, "practical" appears once, and "algorithm" appears zero times. Sort by popularity and break ties in lexicographical order.
Constraints:
1 <= features.length <= 1001 <= features[i].length <= 100features[i] consists of lowercase English letters.features are unique.1 <= responses.length <= 1001 <= responses[i].length <= 100responses[i] consists of lowercase English letters and spaces.responses[i] does not have leading or trailing spaces.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 every single possible way to count how many times each feature is mentioned. We go through each review and look for each feature individually to see if it's there. This ensures we don't miss any mentions, but it can take a long time.
Here's how the algorithm would work step-by-step:
def sort_features_by_popularity_brute_force(features, reviews):
feature_counts = {}
for feature in features:
feature_counts[feature] = 0
# Iterate through each feature.
for feature in features:
# Iterate through each review to count feature mentions.
for review in reviews:
if feature in review:
# Increment count when feature is found.
feature_counts[feature] += 1
# Sort features by counts in descending order.
sorted_features = sorted(feature_counts.items(), key=lambda item: item[1], reverse=True)
# Extract the feature names from the sorted list.
sorted_feature_list = [feature for feature, count in sorted_features]
return sorted_feature_listTo efficiently sort features based on popularity from provided customer reviews, we'll first count how many times each feature is mentioned. Then, we'll use this count to arrange the features in the order of most to least popular, while also respecting any initial ordering constraints.
Here's how the algorithm would work step-by-step:
def sort_features_by_popularity(number_of_features, feature_list, number_of_reviews, reviews):
feature_counts = {feature: 0 for feature in feature_list}
for review in reviews:
review_lower = review.lower()
for feature in feature_list:
feature_lower = feature.lower()
# Count feature mentions. Case-insensitive search.
if feature_lower in review_lower:
feature_counts[feature] += 1
# Sort features by popularity, then by original order.
sorted_features = sorted(
feature_list,
key=lambda feature: (-feature_counts[feature], feature_list.index(feature))
)
# Return the sorted list of features.
return sorted_features| Case | How to Handle |
|---|---|
| Empty feature list or empty feature requests list | Return an empty list or original feature list respectively, as there's nothing to sort or no requests to base the sort on. |
| Null feature list or null feature requests list | Throw an IllegalArgumentException (or equivalent) to indicate invalid input. |
| Feature requests list contains duplicates | Count the occurrences of each feature, so duplicates increment popularity accordingly. |
| Feature requests list contains null or empty strings | Ignore null/empty strings during processing or throw an exception depending on requirements. |
| featureList contains duplicate feature names | Handle the first occurrence, or aggregate popularity from requests for all duplicate feature names. |
| Very large featureList or featureRequestsList causing potential memory issues | Consider using more memory efficient data structures or processing the data in chunks. |
| Feature requests contain feature names not present in featureList | Ignore these irrelevant feature requests, or log them for analysis/reporting if required. |
| Tie in popularity count between multiple features, maintaining original order | Use a stable sorting algorithm to preserve the original order among features with the same popularity count. |