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_vote
is 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).