FastPrepStreamed Planar Item Triplets

Streamed Planar Item Triplets

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Items arrive one at a time. Each item has a two-dimensional integer value [x,y], represented by a row of points in arrival order. Its item ID is its zero-based row index. Equal coordinates still represent different items.

Keep unconsumed items until a group of exactly three fits. For this exercise, assume a group fits only when the Euclidean distance between every pair of its items is strictly less than threshold. A pair at exactly the threshold is excluded.

After each arrival, choose the lexicographically smallest valid triple of available item IDs, written in increasing order, and immediately consume all three items. If no triple fits, retain the new item. Consumed items never become available again. There can be at most one group at an arrival: the preceding state had no fitting triple, so every new fitting triple contains the new item.

Return one row per arrival. The row contains the three consumed IDs if a group forms, or an empty array if it does not. This finite batch simulates the reported class method that stores one arriving item and immediately returns a fitting group. Do not process future arrivals early or flush unmatched items into a group at the end.

For two item IDs i and j, the exact compatibility test is (points[i][0] - points[j][0])^2 + (points[i][1] - points[j][1])^2 < threshold^2. At threshold 0, no pair fits, even at equal coordinates.

Function

packagePointArrivals(points: int[][], threshold: int) → int[][]

Examples

Example 1

points = [[0,0],[1,0],[0,1],[10,10],[11,10],[10,11]]threshold = 2return = [[],[],[0,1,2],[],[],[3,4,5]]

The first three arrivals fit and are consumed at ID 2. The later three fit at ID 5; earlier items cannot be reused.

Example 2

points = [[0,0],[3,0],[1,0],[2,0]]threshold = 3return = [[],[],[],[0,2,3]]

The first three cannot form a group because IDs 0 and 1 are exactly distance 3 apart. At ID 3, the earliest fitting triple is [0,2,3].

Example 3

points = [[3,4],[3,4],[3,4],[3,4]]threshold = 1return = [[],[],[0,1,2],[]]

The first three identical coordinates represent distinct items and form a group. The fourth arrival remains alone.

Constraints

  • For this exercise, assume 0 <= points.length <= 300.
  • Every row contains exactly two integers; for this exercise, assume -10^6 <= x, y <= 10^6.
  • For this exercise, assume 0 <= threshold <= 10^6.
  • Arrival order may be arbitrary, and duplicate coordinates are allowed.

More Google problems

See Google hiring insights
public int[][] packagePointArrivals(int[][] points, int threshold) {
    // write your code here
}
points[[0,0],[1,0],[0,1],[10,10],[11,10],[10,11]]
threshold2
expected[[],[],[0,1,2],[],[],[3,4,5]]
Checking account…