๐Ÿ’ฌ

Social Feed

Model users, posts, comments and likes โ€” and build a feed sorted by newest with like counts.

Project 8Advanced

๐Ÿงฉ 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
โœ๏ธ Pseudocode editor ยท dry-run tool ยท test cases

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

โ† All projects