Design Stack Overflow
❓ What is Stack Overflow? It is a question-and-answer site for programmers. Someone asks a question and tags it (
python,sql). Other people answer, and everyone votes the useful posts up and the bad ones down. The person who asked picks the answer that solved their problem, and every vote and accepted answer adds to or takes from the author's reputation, the number shown next to their name.
This problem looks like a CRUD app at first: store questions, store answers, list them. The interesting part is the bookkeeping around votes. A user can vote, change their mind, and take the vote back, and the score and the author's reputation must be right after every one of those steps. Interviewers use this problem to see whether you model that state carefully, whether you pick inheritance for a good reason, and whether your entry-point class stays thin.
Requirements
"Design Stack Overflow. Users ask questions and answer them, and everyone can vote. Good posts earn reputation."
The prompt names the features but none of the rules. The questions below turn it into rules we can code and test.
Clarifying Questions
You: What can users post? Questions and answers only, or comments too?
Interviewer: Questions with a title and some tags, and answers to questions. Leave comments out for now.
You: How does voting work? Can a user vote twice on the same post, or change their vote?
Interviewer: One vote per user per post, up or down. They can switch it or take it back at any time. Nobody can vote on their own post.
You: How much reputation is a vote worth?
Interviewer: An upvote on a question gives its author 5, an upvote on an answer gives 10, and any downvote costs the author 2. If a vote changes, the old effect is undone. Reputation can go negative in this version.
You: Who can accept an answer, and can it change?
Interviewer: Only the person who asked. One accepted answer per question at a time, and they can switch to another one. The accepted answer's author gets 15. You don't get it for accepting your own answer.
You: In what order are the answers shown?
Interviewer: The accepted answer on top, then by score, highest first. For equal scores, the older answer first.
You: What kind of search do we need?
Interviewer: By tag, newest questions first. Full-text search is a later discussion.
You: Do we need to handle many users voting at the same moment?
Interviewer: Single-threaded for now. We may come back to it.
The third answer is the one that shapes the design. "Changing a vote undoes the old effect" means a vote is not an event we can fire and forget. We have to remember each user's current vote on each post, or we cannot undo it later.
💡 Interview tip: When a prompt has counters that users can change (votes, likes, ratings), always ask "can they change or take it back?" If yes, you must store who did what, not just the totals. That one answer decides your data model.
Final Requirements
- Users register with a unique id and start with reputation 0.
- A user can post a question with a title and a list of tags, and answer any question. Questions and answers get ids from one shared sequence.
- A user can vote a post up or down, change the vote, or take it back. Each user has at most one vote per post, and users cannot vote on their own posts.
- A post's score is its upvotes minus its downvotes.
- Reputation: +5 per upvote on your question, +10 per upvote on your answer, -2 per downvote on any of your posts, +15 for your accepted answer. When a vote or the accepted answer changes, the old effect is undone.
- The author of a question can accept one of its answers, and can switch to another. Accepting your own answer earns nothing.
- A question's answers are listed accepted first, then by score (high to low), then oldest first.
- Users can list the questions with a given tag, newest first.
Out of scope
- Comments (see Design Nested Comments for that tree).
- Editing, closing and deleting posts, bounties and badges.
- Privileges unlocked by reputation (we come back to this in Extensibility).
- Full-text search, persistence and several threads at once.