This question evaluates skills in designing in-memory data structures and algorithms for frequency counting and interval querying, including extracting top-K elements with a heap, within the Coding & Algorithms domain.
Design an in-memory logging component that supports recording log events and querying the top K most frequent messages.
Each log event has:
timestamp
(integer seconds)
message
(string)
Implement the following operations:
log(timestamp, message)
: record a new log event.
topK(startTime, endTime, k) -> List[str]
: return up to
k
messages with the highest frequency among events with
startTime <= timestamp <= endTime
.
message
first (or state and apply a consistent tie-break rule).
k
distinct messages in the interval, return all of them.
log
.
2 * 10^5
total
log()
calls
2 * 10^5
total queries
k
can be up to the number of distinct messages in the queried interval
Logs:
log(1, "A")
,
log(2, "B")
,
log(3, "A")
,
log(10, "C")
Query:
topK(1, 3, 2)
→
["A", "B"]
(A appears 2 times, B appears 1 time)