Closest Pair Among the Latest K Readings
Updated
Problem statement
A monitor receives readings one at a time: nums[0], then nums[1], and so on. After each new reading, it looks only at the latest k readings, or at all readings so far if fewer than k have arrived.
Among those readings, find the closest pair: the smallest value of |a - b| where a and b are two different readings. Two readings with the same value are a pair with difference 0. If there are fewer than two readings to look at, the answer for that moment is -1.
Return an array answer of the same length as nums, where answer[j] is the answer right after reading nums[j] arrives.
Function
closestGaps(nums: int[], k: int) → int[]Examples
Example 1
nums = [40,7,25,9,31,26]k = 3return = [-1,33,15,2,6,5]The latest 3 readings after each arrival are [40], [40, 7], [40, 7, 25], [7, 25, 9], [25, 9, 31] and [9, 31, 26]. The closest pairs are none (-1), 40 and 7 (33), 25 and 40 (15), 7 and 9 (2), 25 and 31 (6), and 26 and 31 (5).
Example 2
nums = [5,12,5,30]k = 3return = [-1,7,0,7]After the third reading the window is [5, 12, 5]; the two 5s are different readings, so the gap is 0. After the fourth reading the first 5 has left: [12, 5, 30] gives 12 - 5 = 7.
Example 3
nums = [8,3]k = 1return = [-1,-1]With k = 1 the window always holds one reading, so there is never a pair: -1 both times.
Constraints
1 <= nums.length <= 20000 <= nums[i] <= 10000000001 <= k <= nums.length
More Rippling Onsite Interview questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delivery Cost TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Maximum Currency Conversion Along a Simple PathPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent WordsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Future-Effective Employee SalaryONSITE INTERVIEW · Seen Aug 2026
- Filter and Sort Scheduled TasksONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Minimum Operations to Make an Array ContinuousAmazon · ONSITE INTERVIEW
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sliding Window Average After Trimming Both EndsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximize Element FrequencyGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Tagged Metric Window SumsDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Sliding Window MedianAuxoAI · OA
- Detect Security Attacks from Network LogsFireworks AI · PHONE SCREEN
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
- Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Maximum Visible DronesAnduril · PHONE SCREENAnduril interview questions