Social Feed
Model users, posts, comments and likes โ and build a feed sorted by newest with like counts.
๐งฉ Problem statement
Posts are records with an id, author, time and text. LIKES is a list of “USER:POST_ID” pairs โ but a user can like a post only once (ignore duplicates). Show the feed newest first as “AUTHOR: TEXT (N likes)”.
๐ Requirements
- Users
- Posts
- Likes (once per user)
- Feed sorted by newest
- Comments (extension)
๐ฅ Inputs
- POSTS (list of records)
- LIKES (list of "user:id")
๐ค Outputs
- Feed lines, newest first
๐ Rules
- A user can like a post only once
๐ง Constraints
- Up to a few thousand posts
๐งญ Suggested approach
- Count unique likes per post with a set of seen pairs
- Sort posts by time (newest first)
- Format each line
๐ง Required concepts
- Maps
- Sets
- Sorting
- Records
Design your solution here. Use Run to dry-run it step by step and Run tests to check it. Hints unlock one at a time โ try on your own first!
๐งช Edge cases to test
- A post with no likes
- The same user liking twice
- Posts with the same time
๐ Complexity analysis
Likes: O(Lยฒ) with a list as the set (O(L) with a real hash set). Feed ordering here is O(Pยฒ); a proper sort makes it O(P log P).
๐ Solution walkthrough (open after youโve tried!)
Duplicate likes are filtered with a set of seen "user:post" pairs, then counted per post with the map-counting pattern. The feed repeatedly picks the newest remaining post (selection-sort style), shows it with its like count (0 if nobody liked it) and removes it.
๐ Challenge extensions
- Add comments per post
- Show only posts from people a user follows
- Rank by likes + recency instead of time only