KatInterview

Fuse Two Sensor Pipelines Into One Object History Log

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designunion-finddesignhash-map

Last updated

Simulate a tracker that links IDs across two systems, stores observations, and returns time-ordered merged histories, testing union-find style grouping and operation replay.

Fusing Two Sensor Pipelines

Two independent pipelines, System A and System B, each follow physical objects and give them their own identifiers. The same real-world object therefore usually has one ID in System A and a different ID in System B. Both pipelines produce observations. Each observation is a dictionary with these keys:

  • timestamp: a number giving when the observation was made,
  • source: either "A" or "B", the pipeline that produced it,
  • source_id: that pipeline's ID for the object,
  • metadata: arbitrary data attached to the observation, which must be returned untouched.

You must simulate a tracker that supports three operations:

  • addLink(a_id, b_id): declares that System A's object a_id and System B's object b_id are the same physical object.
  • addObservation(observation): stores one observation, from either system.
  • getHistory(system, id): returns every observation belonging to the physical object that id denotes within system ("A" or "B").

Your task

Implement a function that receives a list of operation names and a parallel list of argument lists, runs the operations in order, and returns one result per operation.

  • operations[i] is one of "addLink", "addObservation", "getHistory".
  • arguments[i] is the argument list for that operation:
    • addLink: [a_id, b_id]
    • addObservation: [observation] (a list holding one dictionary)
    • getHistory: [system, id]

The result list has the same length as operations:

  • for addLink and addObservation, the entry is None;
  • for getHistory, the entry is a list of observation dictionaries.

Rules

  • Identity is transitive. Links connect IDs into groups. If a1 is linked to b1, and b1 is linked to a2, then a1, b1 and a2 all denote one physical object. An ID may be linked multiple times, and repeating a link has no further effect. An A-ID and a B-ID with the same text are different identities (("A", "x") is not ("B", "x")) unless linked.
  • Order of operations matters, but links apply retroactively. A link may arrive after observations for its IDs were stored. A later getHistory must include all observations stored so far for every ID in the merged group, regardless of when the link was added. A getHistory only reflects operations before it.
  • History contents. The history for a group contains every observation stored so far under any ID of the group, from both systems. Each returned observation keeps its timestamp, source, source_id and metadata exactly as it was added.
  • Ordering. Sort by timestamp ascending. If two observations have equal timestamps, the one that was added earlier (by addObservation call order) comes first.
  • Unknown IDs. If the queried (system, id) has never appeared in any link or observation so far, return an empty list. If it has appeared but has no observations in its group, also return an empty list.
  • Links are never removed or changed.

Constraints

  • 1 <= len(operations) == len(arguments) <= 10^4
  • IDs are strings; system and source are always "A" or "B".
  • Timestamps are numbers (integers or floats); they may arrive out of order and may repeat.
  • metadata can be any JSON-like value.
  • Each observation is added at most once per addObservation call; the same content may be added multiple times and each addition counts as a separate observation.

Examples

Example 1

Input: fuse_object_histories(['addObservation', 'addObservation', 'addLink', 'getHistory'], [[{'timestamp': 2, 'source': 'A', 'source_id': 'a1', 'metadata': {'x': 1}}], [{'timestamp': 1, 'source': 'B', 'source_id': 'b1', 'metadata': {'y': 2}}], ['a1', 'b1'], ['A', 'a1']])

Output: [None, None, None, [{'timestamp': 1, 'source': 'B', 'source_id': 'b1', 'metadata': {'y': 2}}, {'timestamp': 2, 'source': 'A', 'source_id': 'a1', 'metadata': {'x': 1}}]]

Example 2

Input: fuse_object_histories(['addLink', 'addObservation', 'getHistory'], [['a1', 'b1'], [{'timestamp': 5, 'source': 'B', 'source_id': 'b1', 'metadata': 'car'}], ['B', 'b1']])

Output: [None, None, [{'timestamp': 5, 'source': 'B', 'source_id': 'b1', 'metadata': 'car'}]]

Hints

Hint 1

Treat each (system, id) pair as a node. Linking two IDs merges their groups, and a group is exactly one physical object. A disjoint-set (union-find) structure handles chains of links well.

Hint 2

Store each observation under its own (source, source_id) node together with an increasing counter of when it was added. Then a late link does not require rewriting anything: at query time, gather observations from all nodes whose root matches.

Hint 3

For getHistory, find the root of the queried node, collect the entries of all nodes sharing that root, and sort by (timestamp, insertion counter). Return an empty list if the node was never seen.