Track article votes and last three flips
RipplingSoftware Engineer · OnsiteLast updated
This question evaluates a developer's ability to design in-memory data structures that support constant-time vote updates, score aggregation, and recent flip-event tracking while handling vote-change semantics.
Problem
Design an in-memory data structure for voting on articles.
Users can vote on an article with either:
- Thumbs up
- Thumbs down
- No vote (user clears their vote)
A flip is when a user changes their vote from up→down or down→up (clearing a vote is not a flip).
Operations
Implement the following operations efficiently:
vote(user_id, article_id, new_vote)
new_voteis one of{UP, DOWN, NONE}.- Updates the user’s vote for that article.
getScore(article_id)
- Returns
(up_count - down_count)for the article.
getLast3Flips(article_id)
- Returns the last 3 flip events for the article in chronological order (oldest→newest).
- Each flip event should include at least
{user_id, from_vote, to_vote, timestamp_or_sequence}.
Complexity requirements
vote: O(1)getScore: O(1)getLast3Flips: O(1)
Notes
- Multiple users can vote on the same article.
- A user can vote multiple times; only changes may affect counts and flips.
- Assume the system runs in a single process (no persistence required).
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.