← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Design a time-aware counter that supports increment operations and count queries with a time-to-live (TTL) window. Implement a class TTLCounter with the following methods:
increment(key: str, timestamp: int, delta: int = 1) -> None: Record that the given key was incremented by delta at the specified timestamp. Timestamps are strictly increasing across all calls.get_count(key: str, timestamp: int) -> int: Return the total number of increments for the key that occurred within the last TTL seconds of the query timestamp. In other words, sum every increment whose timestamp is ≥ (timestamp - TTL). If the key has no increments in the window, return 0.The TTL is fixed for the lifetime of the counter and is passed to the constructor. All operations must be efficient even when the number of increments per key is large.