KatInterview

Article Reaction Ledger with Recent Reversal Log

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-mapdequesimulation

Last updated

Simulate a stream of up/down/clear reactions on articles, maintaining net scores and a bounded log of the three most recent reversals per article, all in constant time per operation.

Article Reaction Ledger

You are building a small in-memory ledger that tracks reader reactions to articles. Each reader can hold one reaction per article: "UP", "DOWN", or "NONE" (no reaction, which is also the default state for any reader/article pair that has never been voted on).

A reversal (flip) happens when a reader's reaction on an article changes directly from "UP" to "DOWN" or from "DOWN" to "UP". The following are not reversals: setting a first reaction (NONE to UP/DOWN), clearing a reaction (UP/DOWN to NONE), and repeating the reaction already held. Note that UP → NONE → DOWN contains no reversal, because the reader passes through the no-reaction state.

You are given a list of operations to process in order. Implement a function that simulates the ledger and returns one result per operation.

Operations

  • "vote" with arguments [user_id, article_id, new_vote]: set the reader's reaction on that article to new_vote (one of "UP", "DOWN", "NONE"). If it equals the current reaction, nothing changes. The result for this operation is None.
  • "getScore" with arguments [article_id]: the result is the number of "UP" reactions minus the number of "DOWN" reactions currently held on that article. An article that has never been seen has score 0.
  • "getLast3Flips" with arguments [article_id]: the result is a list of up to the 3 most recent reversals on that article, ordered oldest to newest. Fewer than 3 are returned if fewer have occurred; an unknown article gives [].

Reversal records

Each reversal is a dictionary with exactly these keys:

  • "user_id": the reader who reversed their reaction
  • "from_vote": the previous reaction ("UP" or "DOWN")
  • "to_vote": the new reaction ("DOWN" or "UP")
  • "sequence": an integer from a single counter shared by all articles. It starts at 0 and increases by 1 only when a reversal occurs, so the first reversal overall gets 1, the next gets 2, and so on. An individual article's reversals may therefore show gaps in their sequence values.

Task

Write a function that takes the list of operation names and the parallel list of argument lists, processes them in order, and returns the list of results (one entry per operation, in the same order). Each operation should be handled in O(1) time.

Constraints

  • operations and arguments have the same length.
  • Operation names are exactly "vote", "getScore", or "getLast3Flips".
  • user_id and article_id are hashable values (for example strings or integers).
  • new_vote is always one of "UP", "DOWN", "NONE".
  • Everything runs in a single process; no persistence is needed.
  • Each operation must run in O(1) time (amortized, using hashing).

Examples

Example 1

Input: track_reactions(['vote', 'vote', 'getScore', 'getLast3Flips'], [['u1', 'a1', 'UP'], ['u1', 'a1', 'DOWN'], ['a1'], ['a1']])

Output: [None, None, -1, [{'user_id': 'u1', 'from_vote': 'UP', 'to_vote': 'DOWN', 'sequence': 1}]]

Example 2

Input: track_reactions(['vote', 'vote', 'vote', 'vote', 'getScore', 'getLast3Flips'], [[1, 'article', 'UP'], [2, 'article', 'DOWN'], [1, 'article', 'UP'], [2, 'article', 'UP'], ['article'], ['article']])

Output: [None, None, None, None, 2, [{'user_id': 2, 'from_vote': 'DOWN', 'to_vote': 'UP', 'sequence': 1}]]

Example 3

Input: track_reactions(['getScore', 'getLast3Flips'], [['missing'], ['missing']])

Output: [0, []]

Example 4

Input: track_reactions(['vote', 'vote', 'vote', 'vote', 'getLast3Flips'], [['u', 'a', 'UP'], ['u', 'a', 'NONE'], ['u', 'a', 'DOWN'], ['u', 'a', 'UP'], ['a']])

Output: [None, None, None, None, [{'user_id': 'u', 'from_vote': 'DOWN', 'to_vote': 'UP', 'sequence': 1}]]

Hints

Hint 1

Never recompute the score by scanning votes. Keep a running total per article and adjust it by the difference between the reader's old and new contribution (UP = +1, DOWN = -1, NONE = 0).

Hint 2

Look up the reader's current reaction first (default NONE). If it equals the new reaction, do nothing. Otherwise a reversal occurs only when both the old and new reactions are real votes (UP or DOWN).

Hint 3

Keep a per-article queue capped at 3 entries (for example collections.deque with maxlen=3) and one global counter that increments only when a reversal is recorded.