KatInterview

Deduplicate Coordinate List with Index Mapping

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashinghashingdeduplicationindexing

Last updated

Tests the ability to extract distinct elements while preserving first-occurrence order and building an index array for reconstruction.

You're building a data processing pipeline that works with sequences of 3D coordinates. Each coordinate is a list of three numbers [x, y, z]. The input often contains many duplicate coordinates, wasting memory.

Write a function that compresses the coordinate list by:

  1. Extracting all distinct coordinates in the order they first appear
  2. Building an index array where each position points to the corresponding unique coordinate

The function should return a list containing exactly two elements:

  • First element: a list of unique coordinates (each as a list of three numbers)
  • Second element: a list of integer indices

The indices must allow perfect reconstruction: for any position i in the original list, unique_coords[index_array[i]] equals original[i].

Constraints

  • Input length: 1 ≤ n ≤ 1,000,000
  • Each coordinate contains exactly three numeric values
  • Coordinates are considered equal if all three values match exactly
  • Standard floating-point equality applies (IEEE 754)

Examples

Example 1

Input: deduplicate_coordinates([[1, 2, 3], [4, 5, 6], [1, 2, 3]])

Output: [[[1, 2, 3], [4, 5, 6]], [0, 1, 0]]

Example 2

Input: deduplicate_coordinates([[0, 0, 0], [0, 0, 0]])

Output: [[[0, 0, 0]], [0, 0]]

Hints

Hint 1

Use a dictionary to track which coordinates you've already seen and their assigned indices

Hint 2

Tuples are hashable in Python and can serve as dictionary keys, while lists cannot

Hint 3

Process coordinates in a single pass, assigning each new unique coordinate the next available index