Log Aggregator
Beginner Mode
Problem Statement
Design a log aggregation system that records timestamped events grouped by key, and supports counting how many events of a given key occurred within a time range.
This models a common backend pattern: counting error rates, request frequencies, or event occurrences within a sliding window.
Implement the LogAggregator class:
LogAggregator()initializes the system.void record(int timestamp, string key)records an event with the given timestamp and key. Timestamps are guaranteed to be in non-decreasing order across all calls.int count(string key, int start, int end)returns the number of events with the given key that have a timestamp in the inclusive range[start, end].
Additional information
1 <= timestamp <= 10^9keyconsists of lowercase English letters and has length between 1 and 20.- Timestamps across calls to
recordare in non-decreasing order. 1 <= start <= end <= 10^9- Multiple events can share the same timestamp and key.
- At most
50,000calls will be made torecordandcountcombined.
Example 1:
Input:
["LogAggregator", "record", "record", "record", "record", "count", "count", "count"]
[[], [1, "error"], [2, "error"], [3, "warn"], [5, "error"], ["error", 1, 5], ["warn", 1, 5], ["info", 1, 5]]
Output: [null, null, null, null, null, 3, 1, 0]
Explanation:
- Three "error" events at t=1,2,5 and one "warn" at t=3.
count("error", 1, 5)returns 3 (all error events fall in range).count("warn", 1, 5)returns 1.count("info", 1, 5)returns 0 (no "info" events recorded).
Example 2:
Input:
["LogAggregator", "record", "record", "record", "record", "record", "count", "count"]
[[], [1, "a"], [1, "a"], [1, "b"], [2, "a"], [2, "b"], ["a", 1, 1], ["a", 1, 2]]
Output: [null, null, null, null, null, null, 2, 3]
Explanation: Key "a" appears twice at t=1 and once at t=2. Range [1,1] has 2 events, range [1,2] has 3.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Microsoft