Design a Log Storage System That Retrieves Log IDs by Time Range at a Given Granularity
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
You are given several logs, where each log contains a unique ID and a timestamp. The timestamp is a string in the format `Year:Month:Day:Hour:Minute:Second`, for example `2017:01:01:23:59:59`. All fields are zero-padded decimal numbers.
Design a log storage system that can store logs and retrieve the logs whose timestamps fall within a given time range at a specified granularity.
The granularity determines how precise the range comparison is. For example, a granularity of `Day` means that only the `Year`, `Month` and `Day` fields are considered when checking whether a log falls within the range.
Implement a class `LogSystem` with these methods:
- `put(id: int, timestamp: str)` stores the given log with its unique ID and timestamp.
- `retrieve(start: str, end: str, granularity: str) -> List[int]` returns the IDs of the logs whose timestamps fall within the range `[start, end]` at the specified granularity, sorted in ascending order.
Example 1:
```text
log = LogSystem()
log.put(1, "2017:01:01:23:59:59")
log.put(2, "2017:01:01:22:59:59")
log.put(3, "2016:01:01:00:00:00")
log.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Year") -> [1, 2, 3]
log.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00", "Hour") -> [1, 2]
```
At `Year` granularity the range is 2016 to 2017, which contains all three logs. At `Hour` granularity the range runs from hour `2016:01:01:01` to hour `2017:01:01:23`. Log 3 is at hour `2016:01:01:00`, one hour before the start, while logs 1 and 2 are at hours `2017:01:01:23` and `2017:01:01:22`, both inside.
Example 2:
```text
log = LogSystem()
log.put(1, "2017:01:01:23:59:59")
log.put(2, "2017:01:02:23:59:59")
log.retrieve("2017:01:01:23:59:59", "2017:01:02:23:59:59", "Day") -> [1, 2]
```
At `Day` granularity the range is January 1 to January 2, 2017, inclusive, so both logs match.
```hint Comparing timestamps
Every field is zero-padded to a fixed width, and the fields run from most to least significant. Consider what that means for comparing two timestamps as plain strings.
```
```hint Applying the granularity
Work out where each granularity ends inside the timestamp string, and remember that the granularity applies to the range bounds as well as to the stored logs.
```
### Constraints & Assumptions
- `1 <= id <= 500`
- `2000 <= Year <= 2017`
- `1 <= Month <= 12`
- `1 <= Day <= 31`
- `0 <= Hour <= 23`
- `0 <= Minute <= 59`
- `0 <= Second <= 59`
- `granularity` is one of `Year`, `Month`, `Day`, `Hour`, `Minute` or `Second`
- At most `300` calls will be made to `put` and `retrieve`
- Logs can be stored in any time order (Example 1 stores the oldest log last)
### Clarifying Questions to Ask
- If `put` is called twice with the same ID, should the second call replace the first log or is that input invalid?
- Can I assume every timestamp is well formed and zero-padded, or should `put` validate it?
- Will `start` ever be later than `end` at the chosen granularity, and should that return an empty list?
- In a real deployment, how many logs would be stored and how often is `retrieve` called compared with `put`?
### What a Strong Answer Covers
- How timestamps are compared and why that ordering is correct.
- How the granularity is applied to the range bounds and to the stored logs.
- Inclusive bounds at the chosen precision, checked against both examples.
- Time and space complexity of `put` and `retrieve`, including the sort of the returned IDs.
- What changes when the number of stored logs or the rate of `retrieve` calls grows.
### Follow-up Questions
- How would you make `retrieve` faster than a full scan when millions of logs are stored?
- How would you support deleting a log by ID, or expiring logs older than a retention period?
- What would change if timestamps arrived as epoch seconds or in different time zones?
- How would you return the IDs ordered by timestamp, with a tie rule that keeps the output unique?
Overview: Design a log storage class that stores logs with an ID and a Year:Month:Day:Hour:Minute:Second timestamp and returns the sorted IDs of logs inside an inclusive time range at a chosen granularity. It tests timestamp comparison and range handling at different precisions, plus how the design would scale to many stored logs.