Design an ordered logger that flushes completed entries in start-time order
Company: Freshworks
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an ordered logger that tracks the lifecycle of log entries identified by unique ids. Each log entry has a start time and an end time.
When an entry ends, the logger should flush all completed entries (those with both start and end recorded) in ascending order of their start times. However, the logger may only flush entries that form a contiguous prefix of the start-time ordering: if an earlier-started entry has not yet ended, no later entry can be flushed until that earlier entry completes.
Each flushed entry is represented as the string `"id:startTime-endTime"`, and entries are returned in the order they are released. Already-flushed entries must not be flushed again.
Implement the following methods:
- `start(id: int, time: int)`: record the start of a log entry with the given `id` and start `time`.
- `end(id: int, time: int) -> List[str]`: record the end of the log entry with the given `id` and end `time`. Return the newly completed entries formatted as `"id:startTime-endTime"`, in ascending order of start time, but only those forming a contiguous prefix of unflushed entries (no gaps over still-running earlier entries).
Example 1:
```text
Input:
["OrderedLogger", "start", "start", "end", "end"]
[[], [1, 10], [2, 20], [2, 25], [1, 15]]
Output: [null, null, null, [], ["1:10-15", "2:20-25"]]
Explanation:
logger = OrderedLogger() # empty logger
logger.start(1, 10) # entry 1 started at t=10
logger.start(2, 20) # entry 2 started at t=20
logger.end(2, 25) # -> [] (entry 1 still running, can't flush 2 yet)
logger.end(1, 15) # -> ["1:10-15", "2:20-25"] (both flush in start-time order)
```
Example 2:
```text
Input:
["OrderedLogger", "start", "end", "start", "end"]
[[], [1, 0], [1, 5], [2, 10], [2, 12]]
Output: [null, null, ["1:0-5"], null, ["2:10-12"]]
Explanation:
logger = OrderedLogger() # empty logger
logger.start(1, 0) # entry 1 started at t=0
logger.end(1, 5) # -> ["1:0-5"] (no earlier pending entries)
logger.start(2, 10) # entry 2 started at t=10
logger.end(2, 12) # -> ["2:10-12"] (flushed immediately)
```
Example 3:
```text
Input:
["OrderedLogger", "start", "start", "start", "end", "end", "end"]
[[], [1, 10], [2, 20], [3, 30], [3, 35], [1, 15], [2, 25]]
Output: [null, null, null, null, [], ["1:10-15"], ["2:20-25", "3:30-35"]]
Explanation:
logger.end(3, 35) # -> [] (entries 1 and 2 still running)
logger.end(1, 15) # -> ["1:10-15"] (entry 2 still running, so entry 3 waits)
logger.end(2, 25) # -> ["2:20-25", "3:30-35"]
```
```hint What to keep ordered
At any moment only one unflushed entry can unblock the rest: the one with the earliest start time. Pick a structure that hands you that entry cheaply, even if starts arrive out of time order.
```
```hint When to stop
A single `end` call can release several entries or none. Work out the exact condition that ends the flushing loop.
```
### Constraints & Assumptions
- `1 <= id <= 10^5`
- `0 <= time <= 10^9`
- Each `id` passed to `start` is unique.
- Every `end` call refers to an `id` that was previously passed to `start`.
- The total number of `start` and `end` calls is at most `10^4`.
- If two entries share a start time, the one with the smaller `id` comes first in the ordering.
### Clarifying Questions to Ask
- Do `start` calls arrive in nondecreasing time order, or can an entry start with an earlier time than one already recorded?
- If two entries have the same start time, which one flushes first?
- Can `end` be called twice for the same id, or for an entry that has already been flushed?
- Should the logger free flushed entries, or keep a history of them?
### What a Strong Answer Covers
- The structure that keeps unflushed entries ordered by start time, and how ties are broken.
- The flush loop: when it releases an entry, when it stops, and why that enforces the contiguous-prefix rule.
- What state is kept per entry and when it is released after a flush.
- The cost per call, given that one `end` call can flush many entries.
### Follow-up Questions
- If starts always arrive in time order, can you drop the heap, and what does each call cost then?
- How would you add a timeout so that one entry that never ends cannot block every later entry forever?
- How would you make the logger safe when `start` and `end` are called from many threads?
- How would you write flushed entries to a file or a downstream service and keep the order guarantee across a process crash?
Overview: Design a logger that records start and end times for log entries and releases completed entries in start-time order, never skipping past an entry that is still running. It tests choosing an ordered structure for pending entries, writing a flush loop with the right stop condition, and amortized cost analysis.