Implement an article voting tracker
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design efficient stateful data structures and algorithms for tracking user votes, including correct handling of idempotent repeated votes, vote changes, deduplication, and retrieval of the most recent events.
Constraints
- 0 <= len(operations) <= 200000
- voteType is always 'UP' or 'DOWN'
- Each operation should be handled efficiently, ideally in O(1) average time
Examples
Input: [('vote', 'u1', 'a1', 'UP'), ('vote', 'u1', 'a1', 'UP'), ('vote', 'u1', 'a2', 'DOWN'), ('vote', 'u1', 'a1', 'DOWN'), ('getLastVotes', 'u1')]
Expected Output: [[('a1', 'DOWN', 3), ('a2', 'DOWN', 2), ('a1', 'UP', 1)]]
Input: [('vote', 'u1', 'a1', 'UP'), ('vote', 'u2', 'a2', 'DOWN'), ('getLastVotes', 'u3'), ('getLastVotes', 'u2'), ('getLastVotes', 'u1')]
Expected Output: [[], [('a2', 'DOWN', 2)], [('a1', 'UP', 1)]]
Input: [('vote', 'u1', 'a1', 'UP'), ('vote', 'u1', 'a2', 'UP'), ('vote', 'u1', 'a3', 'DOWN'), ('vote', 'u1', 'a4', 'UP'), ('vote', 'u1', 'a2', 'UP'), ('getLastVotes', 'u1')]
Expected Output: [[('a4', 'UP', 4), ('a3', 'DOWN', 3), ('a2', 'UP', 2)]]
Input: [('vote', 'u1', 'a1', 'UP'), ('vote', 'u1', 'a1', 'DOWN'), ('vote', 'u1', 'a1', 'UP'), ('getLastVotes', 'u1')]
Expected Output: [[('a1', 'UP', 3), ('a1', 'DOWN', 2), ('a1', 'UP', 1)]]
Input: []
Expected Output: []
Hints
- Keep the current vote for each (userId, articleId) pair so you can detect whether a new vote is a duplicate or a change.
- Since each query only needs the last 3 events, you only need to keep a small recent history per user.