FastPrepStreaming Triple Packing by Size Threshold
Problems/Streaming Triple Packing by Size Threshold

Streaming Triple Packing by Size Threshold

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source directly provides one-at-a-time item sizes, a stateful class, the strict max-minus-min threshold for three items, immediate triple output, sorted insertion, and adjacent-window checking. Removal after packing, deterministic choice among multiple triples, a finite batch adapter, examples, and bounds are practice scaffolding. The two-dimensional Euclidean-distance follow-up remains separately blocked because its exact acceptance and output contract were not preserved. The judged one-dimensional core matches the visible source at about 88%.

Items arrive on a conveyor belt one at a time. sizes[i] is the nonnegative integer size of the next item.

Keep every unpacked item. After each arrival, if three unpacked sizes a <= b <= c satisfy c - a < threshold, immediately pack and remove one such triple.

When more than one triple is possible, choose the lexicographically smallest sorted triple. Return every packed triple, with each triple sorted, in the order it is packed. Items left unpacked after the final arrival are not returned.

Function

packTriples(sizes: int[], threshold: int) → int[][]

Examples

Example 1

sizes = [5,1,4,3,9,2]threshold = 3return = [[3,4,5]]

After size 3 arrives, the sorted available sizes are [1,3,4,5]. Triple [3,4,5] has range 2, which is strictly below 3, so it is packed. The remaining values never form another valid triple.

Example 2

sizes = [1,2,3,10,11,12]threshold = 3return = [[1,2,3],[10,11,12]]

Each consecutive block of three has range 2 and is packed as soon as its third item arrives.

Example 3

sizes = [0,10,20,11]threshold = 15return = [[0,10,11]]

When 11 arrives, both [0,10,11] and [10,11,20] are valid. The lexicographically smaller triple [0,10,11] is chosen.

Constraints

  • 1 <= sizes.length <= 5000.
  • 0 <= sizes[i] <= 1000000000.
  • 1 <= threshold <= 1000000000.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
public int[][] packTriples(int[] sizes, int threshold) {
    // write your code here
}
Checking account…