← Questions

Access log storage with time range queries

0
GleanCoding

A coding interview question asked at Glean. It tests logging, range queries and design.

Build an in-memory store for user access log entries. Each entry records who accessed which resource and when. The store must answer "which entries fall inside this time range?" much faster than scanning every entry.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • record(user_id: str, timestamp: int, resource: str) -> None — adds an entry. Entries may arrive slightly out of order.
  • query(start: int, end: int) -> list[tuple[str, int, str]] — every entry with start <= timestamp <= end, as (user_id, timestamp, resource), ordered by timestamp, then by arrival order.

Example

record("ana", 100, "/docs/1")
record("bo",  105, "/docs/2")
record("ana", 103, "/search")      # arrives late
record("cy",  120, "/docs/1")
query(100, 105)  ->  [("ana", 100, "/docs/1"), ("ana", 103, "/search"), ("bo", 105, "/docs/2")]
query(106, 119)  ->  []
query(105, 200)  ->  [("bo", 105, "/docs/2"), ("cy", 120, "/docs/1")]

Constraints

  • Up to 10^6 entries and 10^5 queries; timestamps are integers in seconds.
  • Both range ends are inclusive.

Clarifying questions

  • Do entries arrive in timestamp order, nearly in order, or in any order?
  • Are the range ends inclusive?
  • Do queries also filter by user or resource?
  • How long must entries be kept, and how much memory is there?
  • Is it read-heavy or write-heavy?

What a strong answer covers

  • In-order arrival: an append-only list and two binary searches per query, O(log n + k) for k results.
  • Out-of-order arrival: a sorted structure (balanced tree, skip list, or sorted list with insertion) or time buckets (one list per minute or hour) that are sorted lazily when queried.
  • Stable ordering for equal timestamps (an arrival counter as a tiebreak).
  • Retention: drop whole old buckets cheaply.
  • Tests: empty range, range covering everything, equal timestamps, late arrivals at the range edges.

Follow-ups

  • query_user(user_id, start, end) for one user's entries (a per-user index).
  • Count entries per resource in a range without returning them all.
  • The log no longer fits in memory: time-partitioned files with an index, and how a query reads them.
  • Many writers at once: how do you keep record cheap and queries consistent?

Where it's asked

What it tests

logging · range queries · design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding