KatInterview

Unified Entity Log with Mergeable ID Groups

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designunion-finddesignsorting

Last updated

Tests ability to design a data structure that resolves multiple system-assigned IDs to a single entity and returns a chronologically merged observation history.

Problem

A fleet of sensors — cameras, radar units, laser scanners, and others — each assign their own local IDs to the physical objects they detect. The same car driving past might be called "C-1" by a camera and "R-5" by a radar unit. Your job is to build a data structure that keeps a unified observation log for each physical entity, even as new ID equivalences are discovered over time.

Implement a function run_entity_log(operations, arguments) that processes a sequence of commands on an internal EntityLog object and returns one result per command.


Commands

"add_observation" — Log one observation.

Arguments: [system_id, entity_id, timestamp, metadata]

  • system_id (string): the name of the sensor system making the observation (e.g. "CAMERA").
  • entity_id (string): the local ID assigned by that system (e.g. "C-1").
  • timestamp (integer): when the observation occurred.
  • metadata (string): free-form payload.

Returns None.


"link_systems" — Declare that two local IDs refer to the same physical entity.

Arguments: [id1, id2]

After this call, all observations ever recorded under id1 or id2 (and anything already merged with either) belong to one unified entity. The link is transitive: if A and B were linked previously, linking B with C also brings A and C together.

Returns None.


"get_history" — Retrieve the full observation log for the entity associated with a given ID.

Arguments: [any_id]

Returns a list of [timestamp, entity_id, metadata] triples covering every observation ever recorded for that entity (across all local IDs in its group), sorted by:

  1. timestamp ascending
  2. system_id ascending (tie-break)
  3. entity_id ascending (tie-break)
  4. metadata string representation ascending (final tie-break)

If any_id has never been seen, return [].


Constraints

  • 1 ≤ len(operations) ≤ 10 000
  • Each operations[i] is one of "add_observation", "link_systems", "get_history".
  • timestamp is a non-negative integer.
  • system_id, entity_id, and metadata are non-empty strings.
  • The same entity_id may appear in multiple "add_observation" calls, possibly from different system_id values.
  • Calling "link_systems" on two IDs that are already part of the same entity has no effect.
  • Calling "link_systems" with an ID that has no prior observations is valid; future "add_observation" calls for that ID will join the correct entity.
  • len(operations) == len(arguments).

Examples

Example 1

Input: run_entity_log(['add_observation', 'add_observation', 'link_systems', 'get_history', 'get_history'], [['LIDAR', 'L-1', 100, 'loc:0,0'], ['RADAR', 'R-5', 105, 'loc:1,1'], ['L-1', 'R-5'], ['L-1'], ['R-5']])

Output: [None, None, None, [[100, 'L-1', 'loc:0,0'], [105, 'R-5', 'loc:1,1']], [[100, 'L-1', 'loc:0,0'], [105, 'R-5', 'loc:1,1']]]

Example 2

Input: run_entity_log(['add_observation', 'add_observation', 'link_systems', 'link_systems', 'add_observation', 'get_history'], [['CAMERA', 'C-1', 20, 'car'], ['RADAR', 'R-1', 10, 'vehicle'], ['C-1', 'R-1'], ['R-1', 'C-1'], ['LIDAR', 'C-1', 15, 'detected'], ['R-1']])

Output: [None, None, None, None, None, [[10, 'R-1', 'vehicle'], [15, 'C-1', 'detected'], [20, 'C-1', 'car']]]

Hints

Hint 1

Think about what data structure lets you group IDs together, merge groups efficiently, and look up which group any ID belongs to — without scanning all IDs every time.

Hint 2

Store the observation list at the group's representative (root), not at each individual ID. That way a link_systems call just combines two lists instead of scattering records across many places.

Hint 3

After a merge, the combined list needs to be in sorted order before the next get_history call. If each group's list is already sorted, two sorted lists can be merged in linear time rather than re-sorted from scratch — but sorting at read time also works if inserts are the bottleneck.