Level: Unknown Level
Round: Full Journey · Type: System Design · Difficulty: 7/10 · Duration: 60 min · Interviewer: Neutral
Topics: System Design, Data Structures, Algorithms, Hashmaps, Heaps, Concurrency
Location: San Francisco Bay Area
Interview date: 2026-01-06
Question: Design a web API for an article voting system similar to Reddit or StackOverflow. The system should allow users to upvote or downvote articles, track vote flips, and rank articles. The get_most_recent_k_flips method must run in O(k) time.
I was asked to design a system for article voting, similar to Reddit or StackOverflow, focusing on efficient data structures and time complexity.
The core requirement was to implement the following methods for an ArticleVotingSystem class:
` class ArticleVotingSystem: def add_article(self, article_name: str) -> int: """ Creates a new article. Returns a unique article_id. """ pass
def up_vote_article(self, article_id: int, user_id: int) -> None:
"""
User votes UP (+1 score).
- If they already voted UP: Do nothing.
- If they voted DOWN before: Change it to UP (this is a "flip").
"""
pass
def down_vote_article(self, article_id: int, user_id: int) -> None:
"""
User votes DOWN (-1 score).
- If they already voted DOWN: Do nothing.
- If they voted UP before: Change it to DOWN (this is a "flip").
"""
pass
def get_most_recent_k_flips(self, user_id: int, k: int) -> list[int]:
"""
Get the last k articles where the user changed their mind ("flipped").
A flip happens when you go Up -> Down OR Down -> Up.
⚠️ IMPORTANT: This must run in O(k) time, not O(total_flips).
"""
pass
def get_top_k(self, k: int) -> list[int]:
"""
Get the top k articles based on score (upvotes minus downvotes).
The list should start with the highest score.
"""
pass
`
Key constraints and rules included:
get_most_recent_k_flips must run in O(k) time.For example:
` system = ArticleVotingSystem()
a1 = system.add_article("Python Tips") # ID: 1 a2 = system.add_article("Java Guide") # ID: 2 a3 = system.add_article("Go Patterns") # ID: 3 a4 = system.add_article("Rust Basics") # ID: 4
system.up_vote_article(a1, user_id=100) # User 100: upvote 1 system.up_vote_article(a2, user_id=100) # User 100: upvote 2 system.down_vote_article(a1, user_id=100) # User 100: FLIP on 1 (up -> down) system.up_vote_article(a3, user_id=100) # User 100: upvote 3 system.down_vote_article(a2, user_id=100) # User 100: FLIP on 2 (up -> down) system.up_vote_article(a2, user_id=100) # User 100: FLIP on 2 (down -> up)
system.get_most_recent_k_flips(user_id=100, k=3)
system.get_most_recent_k_flips(user_id=100, k=2)
system.get_top_k(2) # Returns: [2, 3] or [3, 2] (both have score +1) `
I discussed using nested dictionaries for O(1) user vote lookups, a list to track vote flips for each user, and a heap for efficiently retrieving the top k articles. I also considered alternative data structures like deques and sorted lists to optimize get_most_recent_k_flips and get_top_k respectively. We also touched on handling vote deletions, tie-breaker scenarios, and concurrency considerations.