Design a Social Network
👥 What is a social network? It is a service where people connect with each other and share things. You send someone a friend request, and once they accept, you see each other's posts. You can like a post or leave a comment, and your home page shows a feed of the newest posts from the people you know.
Interviewers phrase this problem in several ways: "design Facebook", "design Twitter", "design LinkedIn". At the class level they are the same design: users, a graph of connections between them, posts with likes and comments, and a feed built from the graph. They differ in one rule each. Facebook friendships are mutual and need a request. Twitter follows are one-way and need no approval. LinkedIn adds "how far away is this person?" (the 1st, 2nd and 3rd badges). We build the Facebook version, add the LinkedIn degree as a requirement, and show how the Twitter follow model fits in Extensibility.
Do not confuse this with the system design version of the same prompt. There, the interviewer wants databases, caches and fan-out services for millions of users. Here, they want clean classes and correct rules in one process. If the conversation drifts toward servers, say that out loud and point to /system-design/twitter; then come back to the objects.
Requirements
"Design a social network. Users can add friends, write posts, like and comment on posts, and see a news feed."
Every word in that sentence hides a decision. Who has to agree to a friendship? Who can see a post? What order is the feed in? Ask before you draw a single class.
Clarifying Questions
You: Are connections mutual, like Facebook friends, or one-way, like Twitter follows?
Interviewer: Mutual. One user sends a request and the other accepts it. After that, both are friends with each other.
You: What if two people send each other a request at about the same time?
Interviewer: Then they clearly both want it. Make them friends right away.
You: Who can see a post?
Interviewer: The author and the author's friends. Only someone who can see a post may like it or comment on it. If a friendship ends later, the old likes and comments stay.
You: What is in the feed, and in what order?
Interviewer: Your own posts and your friends' posts, newest first. The caller asks for the top k. No ranking by likes for now, but we may want that later.
You: Can a user like the same post twice? Can they unlike?
Interviewer: One like per user per post; a second like is rejected. Skip unlike for now.
You: Do we need anything from LinkedIn, like the "2nd" badge on a profile?
Interviewer: Yes. Given two users, tell me if they are 1, 2 or 3 hops apart. Beyond 3, just say "not close".
You: Do comments have replies?
Interviewer: No, flat comments in the order they were written.
The visibility answer is the one to write down twice. It means every like and comment has to check the friendship graph, and it means the check happens at the moment of the action, not later. That one sentence decides where the can_see rule lives and what happens to old likes after an unfriend.
💡 Interview tip: When the prompt names several products ("like Facebook or Twitter"), ask which connection model you are building before anything else. Mutual friends and one-way follows lead to different classes, and switching halfway through costs you ten minutes.
Final Requirements
- Users can join the network. Each user gets an id.
- A user can send a friend request to another user, and the receiver can accept it. If both users have asked each other, they become friends at once. Friendship is mutual, and either side can end it.
- A user can write a text post.
- A user can like a post once and comment on it, but only if they can see it: they wrote it or they are friends with the author right now.
- Anyone can read a post's like count and its comments, oldest first.
- A user's feed is the newest
kposts written by them or their current friends, newest first. - Given two users, report their degree of connection: 0 (same user), 1, 2 or 3, or "not close" beyond that.
Out of scope
- Groups, pages, messages, photos and video.
- Unlike, editing and deleting posts or comments.
- Feed ranking by engagement (we come back to it as a strategy hook).
- Many servers, storage and caching: that is the system design version.