Design StackOverflow

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-05-02

Problem Statement


🎯 Objective


Design a Q&A platform like StackOverflow, where users can ask questions, post answers, comment, vote, and tag content.


The system should manage user reputation, content moderation, and search functionality efficiently.


πŸ”Ή Context


StackOverflow is a popular community-driven platform where users:


β€’Ask questions related to programming topics.
β€’Post answers to help others.
β€’Upvote or downvote content based on quality.
β€’Use tags to categorize questions.
β€’Gain or lose reputation points through community interactions.


Your task is to design the low-level class structure for this system focusing on object-oriented modeling, relationships, and behaviors β€” not scalability or distributed systems (that would be HLD).


βœ… Functional Requirements


πŸ§‘β€πŸ’» User Features


Post a Question

β€’A user can post a question with title, description, and tags.

Post an Answer

β€’Other users can post answers to a question.

Comment on Question/Answer

β€’Add comments for clarification or feedback.

Upvote/Downvote

β€’Users can vote on questions or answers to indicate usefulness.

Edit/Delete

β€’Users can edit or delete their own questions/answers/comments (within limits).

Search Questions

β€’Search by title, tags, or text content.

View Reputation

β€’Each user’s reputation updates based on community votes.


πŸ§‘β€βš–οΈ Admin/Moderator Features


Close/Flag Questions

β€’Admins can close, reopen, or flag inappropriate content.

Ban or Warn Users

β€’For spamming or violating policies.


βš™οΈ Non-Functional Requirements


RequirementDescription


Extensibility

Easy to add new content types (e.g., discussions, polls).


Maintainability

Clean separation of models β€” Question, Answer, Comment, Vote.


Performance

Efficient searching and filtering (abstract via interface).


Consistency

Votes and reputation must be consistent and correctly updated.


Scalability (Optional)

Can later be extended to distributed architecture.


🧩 Core Entities & Responsibilities


ClassDescription


User

Represents a registered user; can post, comment, and vote.


Question

Represents a question with title, description, tags, and votes.


Answer

Represents an answer linked to a specific question.


Comment

Represents user comments on questions or answers.


Tag

Represents a category/keyword for a question.


Vote

Represents upvote/downvote by a user on content.


ReputationManager

Manages user reputation points based on votes.


SearchService

Handles searching/filtering of questions by keyword or tag.


Moderator

Subclass of User with elevated permissions (close, delete).


🧾 Sample Class Diagram (Text Form)

1 2β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 3β”‚ User β”‚ 4│────────────────────│ 5β”‚ - userId β”‚ 6β”‚ - name β”‚ 7β”‚ - reputation β”‚ 8│────────────────────│ 9β”‚ + postQuestion() β”‚ 10β”‚ + postAnswer() β”‚ 11β”‚ + addComment() β”‚ 12β”‚ + vote() β”‚ 13β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 14 β”‚ 15 β–Ό 16β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 17β”‚ Question β”‚ 18│────────────────────│ 19β”‚ - questionId β”‚ 20β”‚ - title β”‚ 21β”‚ - description β”‚ 22β”‚ - tags: List<Tag> β”‚ 23β”‚ - answers: List<Answer> β”‚ 24β”‚ - votes: List<Vote> β”‚ 25│────────────────────│ 26β”‚ + addAnswer() β”‚ 27β”‚ + addComment() β”‚ 28β”‚ + getVoteCount() β”‚ 29β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 30 31β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 32β”‚ Answer β”‚ 33│────────────────────│ 34β”‚ - answerId β”‚ 35β”‚ - questionId β”‚ 36β”‚ - userId β”‚ 37β”‚ - content β”‚ 38β”‚ - votes: List<Vote>β”‚ 39│────────────────────│ 40β”‚ + addComment() β”‚ 41β”‚ + getVoteCount() β”‚ 42β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 43 44β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 45β”‚ Comment β”‚ 46│────────────────────│ 47β”‚ - commentId β”‚ 48β”‚ - content β”‚ 49β”‚ - createdBy β”‚ 50β”‚ - parentId β”‚ 51β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 52 53β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 54β”‚ Tag β”‚ 55│────────────────────│ 56β”‚ - tagId β”‚ 57β”‚ - name β”‚ 58β”‚ - description β”‚ 59β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 60 61β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 62β”‚ Vote β”‚ 63│────────────────────│ 64β”‚ - voteId β”‚ 65β”‚ - voter: User β”‚ 66β”‚ - value (+1/-1) β”‚ 67β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


🧠 Example Flow


Scenario:


1.User A posts a question β€” β€œHow does garbage collection work in Java?”
2.User B posts an answer explaining GC.
3.User C upvotes the answer.
4.System updates User B’s reputation by +10.


Flow:

1 2UserA.postQuestion(title, desc, tags) 3UserB.postAnswer(questionId, content) 4UserC.vote(answerId, +1) 5ReputationManager.update(UserB, +10)


βš™οΈ Reputation Rules (Example)

ActionReputation Change

Answer Upvoted

+10

Question Upvoted

+5

Answer Downvoted

-2

Question Downvoted

-1

Accepted Answer

+15


🧩 Design Patterns


PatternUse


Observer Pattern

To notify users about new answers/comments on subscribed questions.


Strategy Pattern

For different search strategies (by keyword, tag, date).


Decorator Pattern

To extend content types (e.g., posts with media).


Factory Pattern

For creating Post objects (Question/Answer/Comment).


Singleton Pattern

For ReputationManager or SearchService.


βš™οΈ Sample Pseudo-Interfaces

1 2class Post: 3 id: int 4 author: User 5 content: str 6 created_at: datetime 7 votes: List[Vote] 8 9class Question(Post): 10 title: str 11 tags: List[Tag] 12 answers: List[Answer] 13 14class Answer(Post): 15 parent_question: Question 16 isAccepted: bool


πŸ“ˆ Possible Extensions


β€’Add Badges/Achievements (Gold, Silver, Bronze).
β€’Implement tag-based recommendation of related questions.
β€’Support markdown formatting for posts.
β€’Add notifications when a question you answered receives a new comment.
β€’Integrate AI-based duplicate question detection.


πŸ’¬ Interview Prompt Summary

Design a system like StackOverflow.
Model classes for users, questions, answers, votes, and comments.
Support functionality such as posting, voting, commenting, and reputation management.
Ensure extensibility for tags, moderation, and search features.


Solution

Comments