Design Stack Overflow
Design Stack Overflow
🙏
জয় শ্রী রাম
🕉
Algorithms and Data Structures: [Link]
System Design: [Link]
Low Level Design: [Link]
Frontend Engineering: [Link]
Problem Statement:
Design Stack Overflow using object-oriented principles.
If you haven't used Stack Overflow before, I highly recommend that you visit [Link] and play around to
become familiar with its core features. Click here for wiki.
Solution:
[Link] 1/23
2/28/24, 5:52 PM Design Stack Overflow
Remember that, even though your interviewer might have something very specific for you to design in his/her mind, the onus
is on you to come up with different relevant features for the system once you have gotten a hint on what your interviewer
wants you to design. You need to ask questions, come up with various features of the system, and then validate them with your
interviewer to know if your interviewer is okay with the system having those features or not.
Coming up with various features is not always straightforward. But we would be using the below methodology that would help
us figuring different features in a very systematic way:
Step #1: Actors: Think about all the actors involved. Figure out all the different types of actors involved and all the
different types of users who will be using the system.
Step #2. User Activities and Use Cases: For each type of actors, you need to think through the different features that
they would be interested in using, and different activities that they would be doing.
Step #3. Entities: Determine different core components that will be working together in sync with other to make the
system, as a whole, functional. Entities are often non-living things.
The Actors and Entities become Classes that we would need to design. The user activities and use cases become the
methods or properties.
So in short, what the above frameworks asks us to do is: start with getting an overall high-level sense of the system you
are going to design. Using the information you have so far, start figuring out all user types, and then for each user
type figure out all the relevant user activities. Analysing all the user activities will give you all the entities and
components that are needed for the system to function.
Remember to ask your interviewer clarifying questions at all steps to accomplish your goal. An integral part of design
interviews is to showcase how good you are at requirements gathering and requirements analysis.
Actors:
We have four main actors involved in our system:
Unregistered Users: Anyone can search and view questions. You do not need to be a registered users to do these
operations.
Registered Users: Registered 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.
Moderators: In addition to all the activities that registered users can perform, moderators can close any question.
Admins: In addition to all the activities that registered users can perform, an admins can block or unblock members.
A quick look at a typical Stackoverflow page will help us come up with different relevant user activities and usecases.
[Link] 2/23
2/28/24, 5:52 PM Design Stack Overflow
Any non-member (guest) can search and view questions. However, to add or upvote a question, they have to become a
member.
Members should be able to post new questions.
Members should be able to add an answer to an open question.
Members can add comments to any question or answer.
A member can upvote a question, answer or comment.
Members can flag (i.e, report as spam or abuse or off-topic) a question, answer or comment, for serious problems or
moderator attention.
Any member can add a bounty to their question to draw attention.
Moderators and admins can close or reopen any question.
Members can add tags to their questions. A tag is a word or phrase that describes the topic of the question.
Moderators and admins can close a question.
Admins can block (ban) or unblock a member if the member's behavior id deemed non-compliant to the community
rules.
Remember that during a design interview the onus is on you to be creative and come up with relevant use cases and
user activities for the system that you are designing, and getting them validated by the interviewer before you go
ahead and write code.
Entities:
Here are the main classes of Stack Overflow System:
Question: This class is the central part of our system. It has attributes like Title and Description to define the question. In
addition to this, we will track the number of times a question has been viewed or voted on. We should also track the
status of a question, as well as closing remarks if the question is closed.
Answer: The most important attributes of any answer will be the text and the view count. In addition to that, we will also
track the number of times an answer is voted on or flagged. We should also track if the question owner has accepted an
answer.
Comment: Similar to answer, comments will have text, and view, vote, and flag counts. Members can add comments to
questions and answers.
Tag: Tags will be identified by their names and will have a field for a description to define them. We will also track daily
and weekly frequencies at which tags are associated with questions.
Photo: Questions or answers can have photos.
Bounty: Each member, while asking a question, can place a bounty to draw attention. Bounties will have a total
reputation and an expiry date.
Account: We will have four types of accounts in the system, guest, member, admin, and moderator. Guests can search
and view questions. Members can ask questions and earn reputation by answering questions and from bounties.
Question:
[Link] 3/23
2/28/24, 5:52 PM Design Stack Overflow
We would start our Object Oriented Design with designing the Question class. Let's think about what are the properties
associated with Questions in Stack Overflow.
Questions will have a mandatory title and text. Questions can optionally have one or more photos.
Questions will have a creation date and the time when it was last edited by the question asker.
A Question has a creator or question asker.
Members can answer a Question.
Members can also put comments to a Question.
A Question can be upvoted or downvoted.
A very interesting edge case to consider is that a user can try to upvote or downvote a question multiple times,
but we need to make sure that a member cannot upvote a question that he/she has already upvoted. A user
should also not be able to downvote a question that he/she has already downvoted.
If a downvote is performed followed by an upvote, or an upvote is performed followed by a downvote then the
upvote and downvote cancel each other.
A Question can be reported if a member thinks it is a spam or an abuse to the system.
Question Asker or an Admin can delete a Question.
If a satisfactory solution has been found, a Question can be closed.
If the Question Asker is in urgent need to get solution to his Question, he/she can add Bounty to his/her Question to get
extra attention. A bounty is a special reputation award given to answers. It is funded by the personal reputation of the
user who offers it, and is non-refundable.
Question asker can optionally associate a Question with one or more tags.
Before we jump on to the class implementation of Question class, let's take a look at the Answer and Comment classes.
[Link] 4/23
2/28/24, 5:52 PM Design Stack Overflow
Answer:
Answer entity will have below properties:
Answer entity has a creator who wrote the answer.
Answer will have some text.
Answer will be associated with creation date time indicating when it was created, and it should also have last updated
date time indicating if the member editted the answer and when.
Answer entity can optinally have photos.
Members can upvote or downvote an Answer.
The creator can delete an Answer.
An Answer can be marked as a solution to a question if the Answer satisfied the asker of the question to which this
Answer is associated to.
People can also comment on an Answer to give their own opinion on that answer.
People can report a comment for abuse or spam.
If an answer satisfies the question asker and gets a bounty, then the creator of the answer gets the bounty.
Comment:
Comment entity has a creator who wrote the answer.
Comment will have some text.
Comment will be associated with creation date time indicating when it was created, and it should also have last
updated date time indicating if the member editted the comment and when.
Comment entity can optinally have photos.
Members can upvote or downvote an Comment.
The creator can delete a Comment.
People can report a comment for abuse or spam.
Did you observe anything interesting about Question entity, Comment entity and Answer entity ? They all have the
following properties in common:
They all have associated creators.
They all have associated creation date time and last updated date time.
They all have some associated text.
They all can have optional photos.
Members can upvote or downvote them.
They can be deleted by creator and/or Admin. Admin can also reinstate an entity deleted by them.
We would create an abstract class with all these common properties and then Question, Answer and Comment will
inherit the abstract class. Please try to learn by heart how we are using Object oriented principles like inheritance in our
design. This is super critical.
package [Link];
/**
* Created by Abhishek on 10/14/21.
*/
public enum Status {
OPEN,
CLOSED,
DELETED,
DEFAULT
}
[Link] 5/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
* Created by Abhishek on 10/14/21.
*/
public abstract class TextPhotoBasedEntity { // TextPhotoBasedEntity is an abstract class
// since we will not be creating an object of this class
// without specifying if this is a question, answer or comment.
public TextPhotoBasedEntity(long id, Member creator, String text, List< Photo > photos) {
[Link] = id;
status = [Link];
[Link] = creator;
[Link] = text;
if (photos != null) {
[Link] = photos;
}
numberOfUsersReportedThisEntity = 0;
}
[Link] 6/23
2/28/24, 5:52 PM Design Stack Overflow
[Link] 7/23
2/28/24, 5:52 PM Design Stack Overflow
[Link] 8/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
import [Link];
import [Link];
/**
* Created by Abhishek on 10/12/21.
*/
public class Question extends TextPhotoBasedEntity{
public Question(long id, Member askingMember, String title, String text, List photos, List tags,
status = [Link];
[Link] = title;
[Link] = bounty;
if (tags != null) {
[Link] = tags;
} else {
[Link] = new ArrayList<>();
}
public void close() { // Question Asker or Admin can close a question due to various reasons lik
status = [Link];
}
[Link] 9/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
import [Link];
import [Link];
/**
* Created by Abhishek on 10/12/21.
*/
public class Answer extends TextPhotoBasedEntity{
public Answer(long id, Member creatingMember, String text, List< Photo > photos) {
super(id, creatingMember, text, photos);
[Link] 10/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
import [Link];
/**
* Created by Abhishek on 10/12/21.
*/
public class Comment extends TextPhotoBasedEntity{
Photo:
A Photo will have:
an id
path to where the photo is stored
creation time
member who created it.
[Link] 11/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
/**
* Created by Abhishek on 10/12/21.
*/
public class Photo {
private long id;
private String photoPath;
private long creationDate;
private Member creatingMember;
Bounty:
Properties of Bounty:
Bounty specifies how much reputation a member is going to get if his/her answer satisfies the question asker.
Bounty has an expiration date.
Question Asker can modify the reputation associated with the Bounty.
[Link] 12/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
import [Link];
/**
* Created by Abhishek on 10/14/21.
*/
public class Bounty {
private int reputation;
private Date expirationdate;
Tag:
package [Link];
/**
* Created by Abhishek on 10/15/21.
*/
public class Tag {
private String text;
public Tag(String text) {
[Link] = text;
}
public String getText() {
return text;
}
}
Member:
A Member has following properties:
Member has a Member ID, Name, Display Name or Username, and Email.
The account of the member can be in any of these states: ACTIVE, CLOSED, CANCELED, BLACKLISTED, BLOCKED
A member can earn reputation by giving high quality answers to open questions.
In addition a moderator can close a question, and an admin can close a question, and block or unblock a member.
[Link] 13/23
2/28/24, 5:52 PM Design Stack Overflow
package [Link];
/**
* Created by Abhishek on 10/12/21.
*/
public class Member {
[Link] 14/23
2/28/24, 5:52 PM Design Stack Overflow
// a question asker can give bounty to someone who has satisfactorily answered to his/her questi
public boolean giveBountyTo(int bountyReputation, Member receiver) {
if (bountyReputation <= reputation && [Link] != [Link]) {
reputation -= bountyReputation;
[Link](bountyReputation);
}
return false;
}
// a member receives bounty if his/her answer to a question
// satisfies the question asker and the question asker gives the answerer the bounty
public void receiveBounty(int bountyReputation) {
reputation += bountyReputation;
}
You might be thinking why we did not create Admin class and Moderator class which could inherit Member class since
a moderator or admin posseses all the properties of a member ? The answer to that is, doing that would not have been
a very good design decision, since being Admin or Moderator is not a permanent status. A member can be a
moderator or admin for a certain time period and not necessarily for th entire time span of [Link] membership. So it
only makes sense to keep "being moderator or/and admin" as a property of a member.
Python code:
[Link] 15/23
2/28/24, 5:52 PM Design Stack Overflow
class Status(Enum):
OPEN = 0
CLOSED = 1
DELETED = 2
DEFAULT = 3
------------------------
import time
class TextPhotoBasedEntity:
def __init__(self, id, creator, text, photos):
[Link] = 0
[Link] = None
[Link] = 0
[Link] = 0
[Link] = None
[Link] = None
[Link] = None
[Link] = None
[Link] = 0
[Link] = None
[Link] = id
[Link] = [Link]
[Link] = creator
[Link] = text
photos = []
if photos is not None:
[Link] = photos
[Link] = set()
[Link] = set()
[Link] = round([Link]() * 1000)
[Link] = round([Link]() * 1000)
[Link] = 0
[Link] 16/23
2/28/24, 5:52 PM Design Stack Overflow
def report(self):
[Link] += 1
def getUpvoteCount(self):
return [Link]()
def getDownvoteCount(self):
return [Link]()
def getCreator(self):
return [Link]
def delete(self):
[Link] = [Link]
def getId(self):
return [Link]
def getText(self):
return [Link]
def getCreationDateTime(self):
return [Link]
def getLastUpdated(self):
return [Link]
def getPhotos(self):
return [Link]
def getMembersWhoDownvotedThisEntity(self):
return [Link]
def getMembersWhoUpvotedThisEntity(self):
return [Link]
def getVoteCount(self):
return [Link]() - [Link]()
def getNumberOfUsersReportedThisEntity(self):
return [Link]
def getStatus(self):
return [Link]
------------------------
[Link] 17/23
2/28/24, 5:52 PM Design Stack Overflow
class Question(TextPhotoBasedEntity):
def __init__(self, id, askingMember, title, text, photos, tags, bounty):
self._title = None
self._status = None
self._bounty = None
self._tags = None
self._comments = None
self._answers = None
def close(self):
self._status = [Link]
def getTitle(self):
return self._title
def getStatus(self):
return self._status
def getBounty(self):
return self._bounty
def getTags(self):
return self._tags
def getComments(self):
return self._comments
def getAnswers(self):
return self._answers
---------------------------------------
import time
class Answer(TextPhotoBasedEntity):
def __init__(self, id, creatingMember, text, photos):
self._solvedProblem = False
self._comments = None
self._comments = []
def markAsASolution(self):
self._solvedProblem = True
def isSolvedProblem(self):
return self._solvedProblem
def getComments(self):
return self._comments
-----------------------------------------------
class Comment(TextPhotoBasedEntity):
def __init__(self, commenter, text, photos):
super().__init__(commenter, text, photos)
--------------------------------------------------
import time
class Photo:
def __init__(self, id, photoPath, creator):
self._id = id
self._photoPath = photoPath
self._creationDate = round([Link]() * 1000)
self._creatingMember = creator
def getId(self):
return self._id
def getPhotoPath(self):
return self._photoPath
def getCreationDate(self):
return self._creationDate
def getCreatingMember(self):
[Link] 19/23
2/28/24, 5:52 PM Design Stack Overflow
return self._creatingMember
------------------------------------------------------
class Bounty:
def __init__(self, reputation, expirationDate):
self._reputation = reputation
self._expirationdate = expirationDate
----------------------------------------------------
class Tag:
def __init__(self, text):
self._text = text
def getText(self):
return self._text
----------------------------------------------------
class AccountStatus(Enum):
ACTIVE = 0
CLOSED = 1
CANCELED = 2
BLACKLISTED = 3
BLOCKED = 4
class Member:
self._accountStatus = [Link]
def closeAccount(self):
self._accountStatus = [Link]
def cancelAccount(self):
self._accountStatus = [Link]
def blacklist(self):
self._accountStatus = [Link]
[Link] 20/23
2/28/24, 5:52 PM Design Stack Overflow
def block(self):
self._accountStatus = [Link]
def promoteToAdmin(self):
self._isAdmin = True
def promoteToModerator(self):
self._isModerator = True
def getReputation(self):
return self._reputation
def getId(self):
return self._id
def getStatus(self):
return self._accountStatus
def getName(self):
return self._name
def getDisplayName(self):
return self._displayName
def getEmail(self):
return self._email
Still hungry to learn more? You might find the below chapters interesting:
OOD
[Link] 21/23
2/28/24, 5:52 PM Design Stack Overflow
Game Design
Design BookMyShowFREE
Instructor:
Abhishek Dey
H E L P Y O U R F R I E N D S S AV E 4 0 % O N O U R P R O D U C T S
[Link] 22/23
2/28/24, 5:52 PM Design Stack Overflow
[Link] 23/23