Knowledge Guide
HomeOO & Low-Level DesignOO Design Problems

Design Facebook a social network

Facebook is an online social networking service where users can connect with other users to post and read messages. Users access Facebook through their website interface or mobile apps.

Image
Image

System Requirements

We will focus on the following set of requirements while designing Facebook:

  1. Each member should be able to add information about their basic profile, work experience, education, etc.

  2. Any user of our system should be able to search other members, groups or pages by their name.

  3. Members should be able to send and accept/reject friend requests from other members.

  4. Members should be able to follow other members without becoming their friend.

  5. Members should be able to create groups and pages, as well as join already created groups, and follow pages.

  6. Members should be able to create new posts to share with their friends.

  7. Members should be able to add comments to posts, as well as like or share a post or comment.

  8. Members should be able to create privacy lists containing their friends. Members can link any post with a privacy list to make the post visible only to the members of that list.

  9. Any member should be able to send messages to other members.

  10. Any member should be able to add a recommendation for any page.

  11. The system should send a notification to a member whenever there is a new message or friend request or comment on their post.

  12. Members should be able to search through posts for a word.

Extended Requirement: Write a function to find a connection suggestion for a member.

Use case diagram

We have three main Actors in our system:

Here are the top use cases of our system:

Image
Image

Class diagram

Here are the main classes of the Facebook system:

Image
Image
Image
Image

Activity diagrams

Add work experience to profile: Any Facebook member can perform this activity. Here are the steps to add work experience to a member's profile:

Image
Image

Create a new post: Any Member can perform this activity. Here are the steps for creating a post:

Image
Image

Code

Here is the high-level definition for the classes described above.

Enums, data types, and constants: Here are the required enums, data types, and constants:

java
public enum ConnectionInvitationStatus {
    PENDING,
    ACCEPTED,
    REJECTED,
    CANCELED
}

public enum AccountStatus {
    ACTIVE,
    CLOSED,
    CANCELED,
    BLACKLISTED,
    DISABLED
}

public class Address {
    private String streetAddress;
    private String city;
    private String state;
    private String zipCode;
    private String country;
}

Account, Person, Member, and Admin: These classes represent the different people that interact with our system:

java
// For simplicity, we are not defining getter and setter functions. The reader can
// assume that all class attributes are private and accessed through their respective
// public getter method and modified only through their public setter method.

public class Account {
  private String id;
  private String password;
  private AccountStatus status;

  public boolean resetPassword();
}

public abstract class Person {
  private String name;
  private Address address;
  private String email;
  private String phone;

  private Account account;
}

public class Member extends Person {
  private Integer memberId;
  private Date dateOfMembership;
  private String name;

  private Profile profile;
  private HashSet<Integer> memberFollows;
  private HashSet<Integer> memberConnections;
  private HashSet<Integer> pageFollows;
  private HashSet<Integer> memberSuggestions;
  private HashSet<ConnectionInvitation> connectionInvitations;
  private HashSet<Integer> groupFollows;

  public boolean sendMessage(Message message);
  public boolean createPost(Post post);
  public boolean sendConnectionInvitation(ConnectionInvitation invitation);
  private Map<Integer, Integer> searchMemberSuggestions();
}

public class Admin extends Person {
  public boolean blockUser(Member member);
  public boolean unblockUser(Member member);
  public boolean enablePage(Page page);
  public boolean disablePage(Page page);
}

public class ConnectionInvitation {
  private Member memberInvited;
  private ConnectionInvitationStatus status;
  private Date dateCreated;
  private Date dateUpdated;

  public bool acceptConnection();
  public bool rejectConnection();
}

Profile and Work: A member's profile will have their work experiences, educations, places, etc:

java
public class Profile {
  private byte[] profilePicture;
  private byte[] coverPhoto;
  private String gender;

  private List<Work> workExperiences;
  private List<Education> educations;
  private List<Place> places;
  private List<Stat> stats;

  public boolean addWorkExperience(Work work);
  public boolean addEducation(Education education);
  public boolean addPlace(Place place);
}

public class Work {
  private String title;
  private String company;
  private String location;
  private Date from;
  private Date to;
  private String description;
}

Page and Recommendation: Each page can have multiple recommendations, and members will follow/like pages:

java
public class Page {
   private Integer pageId;
   private String name;
   private String description;
   private String type;
   private int totalMembers;
   private List<Recommendation> recommendation;

   private List<Recommendation> getRecommendation();
 }

 public class Recommendation {
   private Integer recommendationId;
   private int rating;
   private String description;
   private Date createdAt;
 }

Group, Post, Message, and Comment: Members can create posts, comment on posts, send messages and join groups:

java
public class Group {
   private Integer groupId;
   private String name;
   private String description;
   private int totalMembers;
   private List<Member> members;

   public boolean addMember(Member member);
   public boolean updateDescription(String description);
 }

 public class Post {
   private Integer postId;
   private String text;
   private int totalLikes;
   private int totalShares;
   private Member owner;
 }

 public class Message {
   private Integer messageId;
   private Member[] sentTo;
   private String messageBody;
   private byte[] media;

   public boolean addMember(Member member);
 }

 public class Comment {
   private Integer commentId;
   private String text;
   private int totalLikes;
   private Member owner;
 }

Search interface and SearchIndex: SearchIndex will implement Search to facilitate searching of members, groups, pages, and posts:

java
public interface Search {
    public List<Member> searchMember(String name);
    public List<Group> searchGroup(String name);
    public List<Page> searchPage(String name);
    public List<Post> searchPost(String word);
  }

  public class SearchIndex implements Search {
    HashMap<String, List<Member>> memberNames;
    HashMap<String, List<Group>> groupNames;
    HashMap<String, List<Page>> pageTitles;
    HashMap<String, List<Post>> posts;

    public boolean addMember(Member member) {
      if(memberNames.containsKey(member.getName())) {
        memberNames.get(member.getName()).add(member);
      } else {
        memberNames.put(member.getName(), member);
      }
    }

    public boolean addGroup(Group group);
    public boolean addPage(Page page);
    public boolean addPost(Post post);

    public List<Member> searchMember(String name) {
      return memberNames.get(name);
    }

    public List<Group> searchGroup(String name) {
      return groupNames.get(name);
    }

    public List<Page> searchPage(String name) {
      return pageTitles.get(name);
    }

    public List<Post> searchPost(String word) {
      return posts.get(word);
    }
  }

Extended requirement

Here is the code for finding connection suggestions for a member.

There can be many strategies to search for connection suggestions; we will do a two-level deep breadth-first search to find people who have the most connections with each other. These people could be good candidates for a connection suggestion, here is the sample Java code:

import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; import static java.util.Collections.reverseOrder; public class Member extends Person { private Integer memberId; private Date dateOfMembership; private String name; private Profile profile; private HashSet<Integer> memberFollows; private HashSet<Integer> memberConnections; private HashSet<Integer> pageFollows; private HashSet<Integer> memberSuggestions; private HashSet<ConnectionInvitation> connectionInvitations; private HashSet<Integer> groupFollows; public boolean sendMessage(Message message); public boolean createPost(Post post); public boolean sendConnectionInvitation(ConnectionInvitation invitation); private Map<Integer, Integer> searchMemberSuggestions() { Map<Integer, Integer> suggestions = new HashMap<>(); for(Integer memberId : this.memberConnections) { HashSet<Integer> firstLevelConnections = new Member(memberId).getMemberConnections()); for(Integer firstLevelConnectionId : firstLevelConnections) { this.findMemberSuggestion(suggestions, firstLevelConnectionId); HashSet<Integer> secondLevelConnections = new Member(firstLevelConnectionId).getMemberConnections()); for(Integer secondLevelConnectionId : secondLevelConnections) { this.findMemberSuggestion(suggestions, secondLevelConnectionId); } } } // sort by value (increasing count), i.e., by highest number of mutual connection count Map<Integer, Integer> result = new LinkedHashMap<>(); suggestions.entrySet().stream() .sorted(reverseOrder(Map.Entry.comparingByValue())) .forEachOrdered(x -> result.put(x.getKey(), x.getValue())); return result; } private void findMemberSuggestion(Map<Integer, Integer> suggestions, Integer connectionId) { // return if the proposed suggestion is already a connection or if there is a // pending connection invitation if(this.memberConnections.contains(connectionId) || this.connectionInvitations.contains(connectionId)) { return; } int count = suggestions.containsKey(connectionId) ? suggestions.get(connectionId) : 0; suggestions.put(connectionId, count + 1); } }
🤖 Don't fully get this? Learn it with Claude

Stuck on Design Facebook a social network? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Design Facebook  a social network** (OO & Low-Level Design) and want to truly understand it. Explain Design Facebook  a social network from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Design Facebook  a social network** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Design Facebook  a social network** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Design Facebook  a social network** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes