Katkat

Track article votes and last three flips

RipplingSoftware Engineer · Onsite

Last 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:

  1. vote(user_id, article_id, new_vote)
  • new_vote is one of {UP, DOWN, NONE}.
  • Updates the user’s vote for that article.
  1. getScore(article_id)
  • Returns (up_count - down_count) for the article.
  1. 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).