Google questions

Log Deduplication

GoogleMedium

A log collector receives entries. Each entry is a timestamp (an integer) and a message (a string). Entries arrive in an order that has nothing to do with their timestamps. LogDeduper() starts empty, and add(timestamp, message) hands it one entry. The collector keeps at most one entry per message.

Wherever entries are listed, they are [timestamp, message] lists ordered by timestamp, with equal timestamps ordered by message as ordinary strings.

Part 1: Keep the first entry

Implement two methods.

add(timestamp, message) records the entry. If an entry with the same message was added before, the new one is ignored, whatever its timestamp.

retained() returns the kept entries in order (by timestamp, ties by message).

  1. Example 1
    init
    []
    operations
    [["retained",[]],["add",[5,"disk full"]],["add",[3,"timeout"]],["add",[9,"disk full"]],["add",[1,"disk full"]],["add",[3,"boot"]],["retained",[]]]
    Output
    [[],null,null,null,null,null,[[3,"boot"],[3,"timeout"],[5,"disk full"]]]

    Why: disk full first arrives with timestamp 5, so the later entries with timestamps 9 and 1 are dropped. boot and timeout both have timestamp 3 and are ordered by message.

Constraints

  • 1 <= len(operations) <= 10^5
  • 0 <= timestamp <= 10^9
  • 1 <= len(message) <= 50
  • Each operation is one of add, retained.
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Log Deduplication: Google interview question | LoopCoach