Design Log Storage System - Problem
Design Log Storage System

You're tasked with building a sophisticated log storage and retrieval system that can efficiently store timestamped log entries and query them based on flexible time ranges.

Each log entry contains a unique ID (integer) and a timestamp in the format Year:Month:Day:Hour:Minute:Second (e.g., 2017:01:01:23:59:59). All components are zero-padded decimal numbers.

Your system must support:
Storing logs: Add new log entries with their timestamps
Flexible retrieval: Query logs within a time range with configurable precision (granularity)

The key challenge is handling granularity - when granularity is set to "Day", you ignore Hour:Minute:Second components and match entire days. For example, with granularity "Day" from 2017:01:01:23:59:59 to 2017:01:02:00:00:01, you'd find all logs from Jan 1st through Jan 2nd, regardless of specific times.

Example scenario: A server logging system where you need to quickly find all error logs from "last Tuesday" (Day granularity) or "between 2-3 PM yesterday" (Hour granularity).

Input & Output

basic_operations.py — Python
$ Input: LogSystem ls = new LogSystem(); ls.put(1, "2017:01:01:23:59:59"); ls.put(2, "2017:01:02:10:30:00"); ls.put(3, "2017:01:03:00:00:00"); ls.retrieve("2017:01:01:00:00:00", "2017:01:02:23:59:59", "Day");
Output: [1, 2]
💡 Note: With 'Day' granularity, we ignore Hour:Minute:Second components. Log 1 (2017:01:01) and Log 2 (2017:01:02) both fall within the range from 2017:01:01 to 2017:01:02, while Log 3 (2017:01:03) is outside the range.
hour_granularity.py — Python
$ Input: LogSystem ls = new LogSystem(); ls.put(1, "2017:01:01:14:30:00"); ls.put(2, "2017:01:01:15:45:00"); ls.put(3, "2017:01:01:16:00:00"); ls.retrieve("2017:01:01:14:00:00", "2017:01:01:15:59:59", "Hour");
Output: [1, 2]
💡 Note: With 'Hour' granularity, we consider Year:Month:Day:Hour but ignore Minute:Second. Log 1 (hour 14) and Log 2 (hour 15) are within the range 14-15, while Log 3 (hour 16) is outside.
year_granularity.py — Python
$ Input: LogSystem ls = new LogSystem(); ls.put(1, "2016:12:31:23:59:59"); ls.put(2, "2017:01:01:00:00:00"); ls.put(3, "2017:12:31:23:59:59"); ls.retrieve("2017:01:01:00:00:00", "2017:12:31:23:59:59", "Year");
Output: [2, 3]
💡 Note: With 'Year' granularity, we only consider the year component. Both Log 2 and Log 3 are from 2017, matching our query range, while Log 1 from 2016 is outside the range.

Visualization

Tap to expand
Log Storage System ArchitectureLog Storage(ID, Timestamp)1232017:01:01:23:59:59Granularity MappingYear→4, Month→7, Day→10Hour→13, Minute→16, Second→19String Index TruncationQuery Processing1. Truncate query range2. Scan and truncate logs3. String comparison4. Return matching IDsExample: Day Granularity QueryQuery: start='2017:01:01:00:00:00', end='2017:01:02:23:59:59', granularity='Day'Truncation Process:2017:01:01:23:59:59 → 2017:01:012017:01:02:10:30:00 → 2017:01:02String Comparison:'2017:01:01' ✓'2017:01:02' ✓'2017:01:03' ✗Result: [1, 2] - Logs with dates 2017:01:01 and 2017:01:02
Understanding the Visualization
1
Store Logs
Each log entry is stored with its ID and full timestamp, like cataloging books with detailed publication info
2
Query with Granularity
Users can search at different precision levels - by year, month, day, hour, etc., like asking for 'all books from 2017' vs 'books published in March 2017'
3
Smart Truncation
The system truncates timestamps to the requested granularity level before comparison, focusing only on relevant time components
4
Efficient Comparison
Uses optimized string comparison on truncated timestamps to quickly identify matching logs in the specified range
Key Takeaway
🎯 Key Insight: By using string index-based truncation instead of expensive string parsing, we can efficiently handle different granularity levels while maintaining fast O(n) query performance with optimized string operations.

Time & Space Complexity

Time Complexity
⏱️
O(n) per query

Need to check all n logs for each query, but with optimized string operations

n
2n
Linear Growth
Space Complexity
O(n)

Store n log entries with their full timestamps

n
2n
Linearithmic Space

Constraints

  • 1 ≤ id ≤ 106
  • 1 ≤ timestamp.length = 19
  • timestamp format is strictly Year:Month:Day:Hour:Minute:Second
  • All timestamp components are zero-padded to 2 digits (except year which is 4 digits)
  • granularity is one of ["Year", "Month", "Day", "Hour", "Minute", "Second"]
  • At most 300 calls will be made to put and retrieve
Asked in
Amazon 42 Google 35 Microsoft 28 Meta 22
68.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen