Design (LLD) Stack Overflow - Machine Coding Interview

Design (LLD) Stack Overflow - Machine Coding Interview

maxresdefault.jpeg

Stack Overflow

Stack Overflow is one of the largest online communities for developers to learn and share their knowledge. The website provides a platform for its users to ask and answer questions, and through membership and active participation, to vote questions and answers up or down. Users can edit questions and answers in a fashion similar to a wiki.

Features

We will be designing a system with the following requirements:

  1. Any non-member (guest) can search and view questions. However, to add or upvote a question, they have to become a member.
  2. Members should be able to post new questions.
  3. Members should be able to add an answer to an open question.
  4. Members can add comments to any question or answer.
  5. A member can upvote a question, answer or comment.
  6. Members can flag a question, answer or comment, for serious problems or moderator attention.
  7. Any member can add a bounty to their question to draw attention.
  8. Members will earn badges for being helpful.
  9. Members can vote to close a question; Moderators can close or reopen any question.
  10. Members can add tags to their questions. A tag is a word or phrase that describes the topic of the question.
  11. Members can vote to delete extremely off-topic or very low-quality questions.
  12. Moderators can close a question or undelete an already deleted question.
  13. The system should also be able to identify most frequently used tags in the questions.

Rough Solution (LLD-Machine Coding)

Basic Entity

  • Admin: Mainly responsible for blocking or unblocking members.
  • Guest: All guests can search and view questions.
  • Member: Members can perform all activities that guests can, in addition to which they can add/remove questions, answers, and comments. Members can delete and un-delete their questions, answers or comments.
  • Moderator: In addition to all the activities that members can perform, moderators can close/delete/undelete any question.
  • System: Mainly responsible for sending notifications and assigning badges to members.

Final Code

public enum QuestionStatus{
  OPEN,
  CLOSED,
  ON_HOLD,
  DELETED
}

public enum QuestionClosingRemark{
  DUPLICATE,
  OFF_TOPIC,
  TOO_BROAD,
  NOT_CONSTRUCTIVE,
  NOT_A_REAL_QUESTION,
  PRIMARILY_OPINION_BASED
}

public enum AccountStatus{
  ACTIVE,
  CLOSED,
  CANCELED,
  BLACKLISTED,
  BLOCKED
}
public class Account {
  private String id;
  private String password;
  private AccountStatus status;
  private String name;
  private Address address;
  private String email;
  private String phone;
  private int reputation;

  public boolean resetPassword();
}

public class Member {
  private Account account;
  private List<Badge> badges;

  public int getReputation();
  public String getEmail();
  public boolean createQuestion(Question question);
  public boolean createTag(Tag tag);
}

public class Admin extends Member {
  public boolean blockMember(Member member);
  public boolean unblockMember(Member member);
}

public class Moderator extends Member {
  public boolean closeQuestion(Question question);
  public boolean undeleteQuestion(Question question);
}
public class Badge {
  private String name;
  private String description;
}

public class Tag {
  private String name;
  private String description;
  private long dailyAskedFrequency;
  private long weeklyAskedFrequency;
}

public class Notification {
  private int notificationId;
  private Date createdOn;
  private String content;

  public boolean sendNotification();
}
public class Photo {
  private int photoId;
  private String photoPath;
  private Date creationDate;

  private Member creatingMember;

  public boolean delete();
}

public class Bounty {
  private int reputation;
  private Date expiry;

  public boolean modifyReputation(int reputation);
}
public interface Search {
  public static List<Question> search(String query);
}

public class Question implements Search {
  private String title;
  private String description;
  private int viewCount;
  private int voteCount;
  private Date creationTime;
  private Date updateTime;
  private QuestionStatus status;
  private QuestionClosingRemark closingRemark;

  private Member askingMember;
  private Bounty bounty;
  private List<Photo> photos;
  private List<Comment> comments;
  private List<Answer> answers;

  public boolean close();
  public boolean undelete();
  public boolean addComment(Comment comment);
  public boolean addBounty(Bounty bounty);

  public static List<Question> search(String query) {
    // return all questions containing the string query in their title or description.
  }
}

public class Comment {
  private String text;
  private Date creationTime;
  private int flagCount;
  private int voteCount;

  private Member askingMember;

  public boolean incrementVoteCount();
}

public class Answer {
  private String answerText;
  private boolean accepted;
  private int voteCount;
  private int flagCount;
  private Date creationTime;

  private Member creatingMember;
  private List<Photo> photos;

  public boolean incrementVoteCount();
}

Did you find this article valuable?

Support Subhahu Jain by becoming a sponsor. Any amount is appreciated!