Retrieve Service Log Entries by Time Range and Granularity

Last updated: November 29, 2026

Quick Overview

Design a log store with `put(log_id, timestamp)` where timestamps look like `2026:11:28:23:59:59`, and `retrieve(start, end, granularity)` returning ids whose timestamps fall in the range when both bounds are truncated to the given unit (Year, Month, Day, Hour, Minute, or Second).

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Design a log store with `put(log_id, timestamp)` where timestamps look like `2026:11:28:23:59:59`, and `retrieve(start, end, granularity)` returning ids whose timestamps fall in the range when both bounds are truncated to the given unit (Year, Month, Day, Hour, Minute, or Second).

An onsite design-a-structure question in Amazon's style, echoing how operators query service logs by hour or day while debugging an incident. The interviewer checks that you exploit the fixed-width format for string comparison and that you can improve on a linear scan with a sorted index.

What the Interviewer Expects
  • Notices fixed-width zero-padded timestamps compare correctly as strings
  • Truncates bounds to the granularity prefix length
  • Makes the end bound inclusive of the whole unit
  • Offers a sorted list with binary search to avoid full scans
  • Returns ids in a defined order
Key Topics to Cover
design a data structure
string comparison
binary search
time series
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support logs arriving out of order at high volume?
  • How would you store a year of logs that do not fit in memory?
  • How would you add filtering by service name efficiently?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Timestamps are fixed width, zero padded, colon separated, from year to second. retrieve returns ids with start truncated ≤\le timestamp truncated ≤\le end truncated, ordered by time....

High-Level Design

Because every field is zero padded and ordered from most to least significant, lexicographic order equals chronological order. Truncating to a granularity is just taking a prefix: Year keeps 4 chara...


Submit Your Answer
Markdown supported