"Sort Features by Popularity" is a string processing and ranking problem often presented in a real-world context. You are given a list of feature names and a list of user responses (sentences). A feature's popularity is defined by the number of unique responses in which it appears.
Your task is to sort the features based on their popularity in descending order. If two features have the same popularity, they should maintain their original relative order from the input feature list (a stable sort). This problem requires careful counting and efficient string searching.
Amazon frequently uses this coding problem because it mirrors actual tasks engineers face, such as analyzing customer feedback or logs. It tests your ability to use Hash Tables for frequency counting and your understanding of string matching (splitting sentences into words). It also checks if you can implement a stable sort, which is a common requirement in ranking systems where ties must be broken consistently.
The pattern involves frequency counting and custom sorting:
Features: ["storage", "battery", "hover"] Responses: ["Battery is good", "I want more storage", "Battery and storage"]
A common error is counting a feature multiple times if it appears twice in the same response (e.g., "Battery is better than the old battery"). The problem usually specifies unique responses. Another mistake is inefficient string matching—searching for each feature in every response string can be slow. Splitting responses into sets of words is much faster. Finally, using an unstable sorting algorithm can lead to incorrect results when popularity counts are tied.
When tackling the "Sort Features by Popularity interview question," focus on preprocessing. Mention that you would convert all words to lowercase if the problem is case-insensitive. Also, discuss the time complexity: where is responses, is words per response, and is features. This shows you are thinking about how the solution scales with large feedback datasets.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| High-Access Employees | Medium | Solve | |
| Invalid Transactions | Medium | Solve | |
| Group Anagrams | Medium | Solve | |
| Alert Using Same Key-Card Three or More Times in a One Hour Period | Medium | Solve | |
| Before and After Puzzle | Medium | Solve |