Streaming Triple Packing by Size Threshold
Updated
Problem statement
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 Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Maximize the Minimum Distance Between RobotsAmazon · ONSITE INTERVIEW
- Maximum Total Length of Non-Overlapping IntervalsBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Room Occupancy at Query TimesWhatNot · ONSITE INTERVIEWWhatNot interview questions
- HTTP Log QueriesDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Smallest Common Integer in Sorted ListsMotive · ONSITE INTERVIEWMotive interview questions
- Russian Doll EnvelopesInMobi · ONSITE INTERVIEWInMobi interview questions
- Find Unmet Users With Shared InterestsPalantir · ONSITE INTERVIEWPalantir interview questions
- Count Later Lower ReadingsRippling · ONSITE INTERVIEWRippling interview questions
- Russian Doll EnvelopesAirbnb · ONSITE INTERVIEWAirbnb interview questions