Design article voting and flip-tracking system
RipplingSoftware Engineer · Technical ScreenLast updated
This question tests your ability to design a stateful system for tracking user interactions (upvotes/downvotes) on content while maintaining an audit trail of user activity.
Design an Article Voting System
Design a backend service to manage voting on articles. The system must track the current score for each article and maintain a history of all voting actions performed by users.
Requirements
- Voting: Users can upvote or downvote an article. Each user can have only one active vote per article; casting a new vote replaces their previous one (e.g., changing from upvote to downvote).
- State: The system must provide the current net score (upvotes minus downvotes) for any given article.
- History: The system must maintain an audit log of every vote change, recording the user ID, article ID, vote value (+1, -1, or 0 for removed), and a timestamp.
Task
Design the class structure or data model and the interface methods to support the following:
vote(user_id, article_id, value)get_score(article_id)get_history(article_id)
Example
- User A upvotes Article 1 (Score: 1)
- User B downvotes Article 1 (Score: 0)
- User A changes vote to downvote (Score: -1)
Considerations
- How will you handle race conditions if multiple requests arrive simultaneously for the same article?
- What data structures are most efficient for frequent score lookups vs. audit log storage?
Hints
Hint 1
Use a hash map to keep track of the current state of votes per user/article to ensure O(1) lookups for current scores.
Hint 2
Consider maintaining a secondary data structure specifically for the aggregate score that updates whenever a vote is cast to avoid re-calculating from the log.
Hint 3
Use a list or log-structured storage to append voting events chronologically to satisfy the history requirement.
Follow-up questions
- How would you optimize the `get_score` operation if the articles receive millions of votes per second?
- If you needed to implement a 'top 10 articles' leaderboard, which data structures would you add?
- How would you ensure atomicity when updating the total score and the user's individual vote record simultaneously?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.