call(back)
Algorithms & Data Structuresmedium

Violation Log Analyzer

Trust & Safety receives a stream of violation events, each (timestamp, user_id, violation_type), with timestamps arriving in non-decreasing order. Design and implement a class supporting:

log.record(timestamp, user_id, violation_type)      # ingest one event
log.count_recent(user_id, window)   # violations by user within the last window
                                    # seconds, measured from the latest timestamp seen
log.top_k(k)                        # top-k users by all-time violation count,
                                    # ties broken lexicographically -> [(user, count), ...]
log.should_ban(user_id, max_violations, window)
                                    # True if the user ever had >= max_violations
                                    # within ANY window of window seconds

Conventions for the judge: timestamps are integer seconds, and a window of W seconds ending at time T covers the half-open interval (T - W, T] — an event exactly W seconds old is outside it. count_recent measures from the latest timestamp seen so far; should_ban considers the user's entire history. So with window = 10, events at t = 0 and t = 9 fit in one window, while events at t = 0 and t = 10 do not.

Asked at