Design Log Storage System in Python

A log storage system manages logs with unique IDs and timestamps. Each timestamp follows the format Year:Month:Day:Hour:Minute:Second (e.g., "2019:01:01:23:59:59") with zero-padded decimal numbers.

We need to implement two main functions:

  • put(id, timestamp) − Stores a log with its unique ID and timestamp

  • retrieve(start, end, granularity) − Returns log IDs within a time range based on specified granularity (Year, Month, Day, Hour, Minute, Second)

How Granularity Works

The granularity parameter determines the precision level for comparison. For example, with granularity "Day", timestamps are compared only up to the day level, ignoring hours, minutes, and seconds.

Timestamp: 2019:01:15:14:30:45 Year 2019 Month 01 Day 15 Hour 14 Minute 30 Second 45 Granularity "Day" compares: 2019:01:15 Granularity "Hour" compares: 2019:01:15:14

Implementation

class LogSystem:
    def __init__(self):
        self.logs = []
    
    def put(self, log_id, timestamp):
        """Store a log with its ID and timestamp"""
        self.logs.append((log_id, timestamp))
    
    def retrieve(self, start, end, granularity):
        """Retrieve log IDs within time range based on granularity"""
        # Map granularity to string slice index
        granularity_map = {
            'Year': 4,    # 2019
            'Month': 7,   # 2019:01
            'Day': 10,    # 2019:01:01
            'Hour': 13,   # 2019:01:01:23
            'Minute': 16, # 2019:01:01:23:59
            'Second': 19  # 2019:01:01:23:59:59
        }
        
        index = granularity_map[granularity]
        start_prefix = start[:index]
        end_prefix = end[:index]
        
        result = []
        for log_id, timestamp in self.logs:
            timestamp_prefix = timestamp[:index]
            if start_prefix <= timestamp_prefix <= end_prefix:
                result.append(log_id)
        
        return result

# Example usage
log_system = LogSystem()

# Store logs
log_system.put(1, "2019:01:01:23:59:59")
log_system.put(2, "2019:01:01:22:59:59") 
log_system.put(3, "2018:01:01:00:00:00")

# Retrieve by year (2018-2019)
year_result = log_system.retrieve("2018:01:01:01:01:01", "2019:01:01:23:00:00", "Year")
print("Year granularity:", year_result)

# Retrieve by hour (more precise range)
hour_result = log_system.retrieve("2018:01:01:01:01:01", "2019:01:01:23:00:00", "Hour")
print("Hour granularity:", hour_result)
Year granularity: [1, 2, 3]
Hour granularity: [1, 2]

How It Works

The system uses string slicing to compare timestamps at different granularity levels:

  • Year granularity − Compares only "2019" vs "2018", so all logs match the range

  • Hour granularity − Compares "2019:01:01:23" format, excluding log 3 which has hour "00"

Time Complexity

Operation Time Complexity Space Complexity
put() O(1) O(1)
retrieve() O(n) O(k)

Where n is the number of logs and k is the number of matching results.

Conclusion

This log storage system efficiently handles timestamp-based queries using string slicing for different granularity levels. The design provides O(1) insertion and O(n) retrieval, making it suitable for moderate-sized log datasets.

Updated on: 2026-03-25T12:23:48+05:30

447 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements