FastPrepClosest Pair Among the Latest K Readings
Problems/Closest Pair Among the Latest K Readings

Closest Pair Among the Latest K Readings

Rippling logoRippling● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: After each reading we report the smallest difference between two different readings among the latest k, or -1 while fewer than two are available. Reporting the difference instead of the pair, the -1 rule, values from 0 to 10^9, the limits and the examples are our own choices.

A review for a Software Engineer role in Bengaluru, posted December 10, 2023, gives this as the question from its contest-style algorithm rounds.

The coding task is a closest-pair question over a sliding window of the latest k values in a stream of integers, answered after every new value. It does not say how to report the pair, how to break ties, or what to output before two values have arrived.

Our version is about 75% the same as the reported question.

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 <= 2000
  • 0 <= nums[i] <= 1000000000
  • 1 <= 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

Interview reports that mention this question

  • Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
  • Rippling SWE Interviews, No Level: 156 Experiences (Part 1 of 6)

Rippling interview experiences

  • Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
  • Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
  • Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
  • Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)
All Rippling interview questionsSee Rippling hiring insights
Saved
class Solution {
    public int[] closestGaps(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…