Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

2D Array Compression With Indices

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is 2D Array Compression With Indices. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Applied Intuition simulation assets may store repeated 2D vertices. Implement a compression function that keeps one copy of each distinct vertex and replaces every original vertex with the index of its retained copy.

Preserve the order in which unique vertices first appear. Two vertices are equal when their coordinate lists contain the same values in the same order.

Formal Specification

Given vertices, a list of n coordinate lists, return a two-element list:

  1. unique_vertices: the distinct coordinate lists in first-occurrence order.
  2. indices: a list of length n, where indices[i] is the index of vertices[i] in unique_vertices.

Each coordinate list has the same positive dimension, and coordinates are integers. Do not mutate the input.

Constraints

  • 0 <= len(vertices) <= 100,000
  • 1 <= len(vertices[i]) <= 8 when vertices is non-empty
  • -10^9 <= vertices[i][j] <= 10^9
  • All coordinate lists have the same dimension
  • The input must not be mutated

Function Signature

def compress_vertices(vertices):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output