Google questions

RPC Timeout Monitor

GoogleOnsiteMedium

A service writes a log record when a remote call starts and another when it ends. TimeoutMonitor(timeout) receives the records one at a time through add(call_id, timestamp, kind), where kind is "start" or "end". Timestamps never decrease, every call has one start record, and at most one end record that comes after it.

A call's running time is end - start once it has ended, and otherwise now - start, where now is the timestamp of the most recent record. A call has timed out when its running time is strictly greater than timeout.

Part 1: Calls that timed out

Implement timed_out(), which returns the ids of all calls that have timed out so far, ordered by start timestamp and then by id. A call that has already ended still counts if its final running time was greater than timeout. add returns None.

  1. Example 1
    init
    [5]
    operations
    [["add",[7,0,"start"]],["add",[8,2,"start"]],["add",[7,4,"end"]],["add",[9,5,"start"]],["timed_out",[]],["add",[8,10,"end"]],["timed_out",[]],["add",[9,12,"end"]],["timed_out",[]]]
    Output
    [null,null,null,null,[],null,[8],null,[8,9]]

    Why: With timeout 5: call 8 runs from 2 to 10, which is 8 and so timed out. After its end record the answer is [8]. Call 9 runs from 5 to 12, which is 7, so the final answer is [8, 9]. Call 7 took 4 and never counts.

Constraints

  • 0 <= timeout <= 10^9
  • 1 <= len(operations) <= 2 * 10^5
  • 0 <= call_id, timestamp <= 10^9
  • call_id values are distinct
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
RPC Timeout Monitor: Google interview question | LoopCoach