Sort Features by Popularity - Problem

You're a product manager analyzing user feedback for your latest software release! ๐Ÿš€

You have a list of features in your product and a collection of user responses from surveys. Each response contains space-separated feature names that users mentioned they liked.

Your goal: Rank features by popularity to prioritize development efforts!

๐Ÿ“Š Popularity Rules:

  • A feature's popularity = number of responses that mention it
  • If a user mentions the same feature multiple times in one response, count it only once
  • Sort features in descending order by popularity
  • If two features have the same popularity, maintain their original order from the features array

Example: If features = ["photoSharing", "videoCall", "chat"] and responses = ["photoSharing videoCall", "videoCall chat chat"], then "videoCall" appears in 2 responses (most popular), while "photoSharing" and "chat" each appear in 1 response. Since "photoSharing" comes before "chat" originally, the result is ["videoCall", "photoSharing", "chat"].

Input & Output

example_1.py โ€” Basic Feature Ranking
$ Input: features = ["photoSharing", "videoCall", "chat"] responses = ["photoSharing videoCall", "videoCall chat chat"]
โ€บ Output: ["videoCall", "photoSharing", "chat"]
๐Ÿ’ก Note: videoCall appears in 2 responses (most popular). photoSharing and chat each appear in 1 response, but photoSharing comes first in the original order.
example_2.py โ€” Equal Popularity Tie-Breaking
$ Input: features = ["search", "messaging", "notifications"] responses = ["search messaging", "notifications search"]
โ€บ Output: ["search", "messaging", "notifications"]
๐Ÿ’ก Note: All features appear in exactly 1 response each. Since they have equal popularity, we maintain the original order from the features array.
example_3.py โ€” Single Response Multiple Mentions
$ Input: features = ["like", "comment", "share"] responses = ["like like comment share share share"]
โ€บ Output: ["like", "comment", "share"]
๐Ÿ’ก Note: Even though 'like' appears twice and 'share' appears three times in the response, each feature is only counted once per response. All have popularity 1, so original order is preserved.

Visualization

Tap to expand
Feature Popularity Analysis Workflow๐Ÿ“Š Data CollectionUser Responses"photoSharing videoCall""videoCall chat chat"๐Ÿ”ข Counting PhaseHash Map BuildingphotoSharing: 1videoCall: 2, chat: 1๐Ÿ“ˆ Ranking PhaseSort by PopularityvideoCall (2) โ†’ #1photoSharing, chat (1) โ†’ #2,#3๐ŸŽฏ ResultFinal Ranking1. videoCall2. photoSharing3. chat๐Ÿ” Detailed Process VisualizationStep 1: Response ProcessingResponse 1: "photoSharing videoCall"Set: {photoSharing, videoCall}Count: +1 eachResponse 2: "videoCall chat chat"Set: {videoCall, chat}Count: +1 eachStep 2: Hash Map StatephotoSharing: 1videoCall: 2chat: 1Final counts ready for sortingStep 3: Sorting LogicvideoCall (pop=2, idx=1)photoSharing (pop=1, idx=0)chat (pop=1, idx=2)Sort by: (-popularity, original_index)
Understanding the Visualization
1
Data Collection
Gather all user responses (like collecting feedback cards from restaurant customers)
2
Counting Phase
Build popularity hash map by processing each response and counting unique feature mentions
3
Ranking Phase
Sort features by popularity (descending) and original index (ascending) for tie-breaking
4
Result Generation
Return ranked feature list for product management decisions
Key Takeaway
๐ŸŽฏ Key Insight: Separate data collection from decision making - build complete popularity statistics first, then sort efficiently using pre-computed data.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nร—mร—k)

n features ร— m responses ร— k average words per response. Each feature requires scanning all responses.

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Only storing the result list with n tuples, no additional hash maps needed.

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค features.length โ‰ค 104
  • 1 โ‰ค features[i].length โ‰ค 10
  • features[i] contains only lowercase letters
  • 1 โ‰ค responses.length โ‰ค 103
  • 1 โ‰ค responses[i].length โ‰ค 103
  • responses[i] contains only lowercase letters and spaces
  • All feature names are unique
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 25
52.0K Views
High Frequency
~18 min Avg. Time
1.8K 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