← Questions

Log manager that evicts the least important logs

0
GoogleCodingLast asked Aug 2025

A coding interview question asked in Google's first round (coding), last asked Aug 2025. It tests heaps and data structure design.

Design an in-memory log manager that holds at most total_max logs. Each log has an id, an importance and a timestamp. When adding a log would go over the limit, the manager first deletes the least important log, and the oldest among equally important ones. Adds and evictions must be fast.

Interface

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

  • LogManager(total_max: int).
  • add(log_id: str, importance: int, timestamp: int, message: str) -> list[str] — store the log and return the ids evicted to make room (usually zero or one).
  • get(log_id: str) -> str | None — the message, or None if missing or evicted.
  • delete(log_id: str) -> bool — remove a log explicitly.
  • recent(n: int) -> list[str] — the ids of the n newest logs, newest first.

Example

m = LogManager(3)
m.add("a", 2, 1, "...")   ->  []
m.add("b", 1, 2, "...")   ->  []
m.add("c", 1, 3, "...")   ->  []
m.add("d", 3, 4, "...")   ->  ["b"]      # importance 1 is the lowest; b is older than c
m.add("e", 1, 5, "...")   ->  ["c"]      # e is stored, then c (importance 1, older than e) goes
m.get("b")                ->  None
m.recent(2)               ->  ["e", "d"]

Constraints

  • Up to 10^6 logs held and 10^5 operations per second.
  • Importance is a small integer range (for example 0 to 9) in the base version.

Clarifying questions

  • When the new log is itself the least important, is it stored and something else evicted, or rejected?
  • Is the importance range small and fixed, or arbitrary?
  • Do timestamps always increase?
  • Is the manager used from several threads?

What a strong answer covers

  • A hash map from id to log node for O(1) lookup and delete.
  • With a small importance range: one doubly linked list (or deque) per level, ordered by time; eviction takes the head of the lowest non-empty level, O(1) with a pointer to the lowest level. That meets the O(1) goal.
  • With arbitrary importance: a min-heap keyed by (importance, timestamp) with lazy deletion, or an ordered set, at O(log n) per operation.
  • A second time-ordered list for recent, sharing the same nodes.
  • The rule for the new log being the least important, decided and tested.
  • Thread safety with one lock, and the cost of that at high rates.

Follow-ups

  • Limit by total bytes instead of count.
  • Logs also expire after a time-to-live.
  • Persist logs to disk and recover the manager after a crash.

Where it's asked

  • Google · First round: coding · Last asked Aug 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

heaps · data structure 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