Design Log Storage System - Problem
You are given several logs, where each log contains a unique ID and timestamp. Timestamp is a string that has the following format: Year:Month:Day:Hour:Minute:Second, for example, 2017:01:01:23:59:59. All domains are zero-padded decimal numbers.
Implement the LogSystem class:
LogSystem()Initializes the LogSystem object.void put(int id, string timestamp)Stores the given log (id, timestamp) in your storage system.int[] retrieve(string start, string end, string granularity)Returns the IDs of the logs whose timestamps are within the range from start to end inclusive.startandendall have the same format as timestamp, andgranularitymeans how precise the range should be (i.e. to the exact Day, Minute, etc.).
For example, start = "2017:01:01:23:59:59", end = "2017:01:02:23:59:59", and granularity = "Day" means that we need to find the logs within the inclusive range from Jan. 1st 2017 to Jan. 2nd 2017, and the Hour, Minute, and Second for each log entry can be ignored.
Input & Output
Example 1 — Basic Log System Operations
$
Input:
[["LogSystem"], ["put", 1, "2017:01:01:23:59:59"], ["put", 2, "2017:01:01:22:59:59"], ["put", 3, "2016:01:01:00:00:00"], ["retrieve", "2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year"], ["retrieve", "2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour"]]
›
Output:
[null, null, null, null, [3,2,1], [2,1]]
💡 Note:
LogSystem created, logs stored, then retrieved by Year granularity (ignores month/day/hour details) and Hour granularity (more precise filtering)
Example 2 — Day Granularity Filter
$
Input:
[["LogSystem"], ["put", 1, "2017:01:01:23:59:59"], ["put", 2, "2017:01:02:23:59:59"], ["put", 3, "2017:01:03:23:59:59"], ["retrieve", "2017:01:01:00:00:00", "2017:01:02:23:59:59", "Day"]]
›
Output:
[null, null, null, null, [1,2]]
💡 Note:
Day granularity means compare only YYYY:MM:DD parts, so logs from Jan 1st and Jan 2nd match the range, Jan 3rd is excluded
Example 3 — Month Granularity
$
Input:
[["LogSystem"], ["put", 1, "2017:01:15:10:30:45"], ["put", 2, "2017:02:20:12:45:30"], ["retrieve", "2017:01:01:00:00:00", "2017:01:31:23:59:59", "Month"]]
›
Output:
[null, null, null, [1]]
💡 Note:
Month granularity compares YYYY:MM only, so only log from January 2017 matches
Constraints
- 1 ≤ id ≤ 106
- 2000 ≤ Year ≤ 2017
- 1 ≤ Month ≤ 12
- 1 ≤ Day ≤ 31
- 0 ≤ Hour ≤ 23
- 0 ≤ Minute, Second ≤ 59
- granularity is one of ["Year", "Month", "Day", "Hour", "Minute", "Second"]
Visualization
Tap to expand
Understanding the Visualization
1
Input
Store logs with ID and timestamp, then query by range and granularity
2
Process
Truncate timestamps based on granularity level and compare ranges
3
Output
Return IDs of logs within the specified range
Key Takeaway
🎯 Key Insight: Truncate timestamps based on granularity level to simplify range comparisons and ignore irrelevant time precision
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code