FastPrepClosest Numbers
Problems/Closest Numbers

Closest Numbers

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEOA

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported from a Headlands Technologies online assessment for a full-time quantitative researcher role that the candidate had applied to online. The assessment had 17 questions on probability, statistics and modeling, quantitative finance, and three coding tasks; this is one of the coding tasks. The task gives 2 to 100,000 integers with no repeats. Find the smallest absolute difference between two of them and output every pair that has it: each pair starts with its smaller number, and the pairs are listed from smallest to largest. The candidate did not pass.

The report asks to print the pairs; here they are returned as a list of pairs. The report gives no value range, so we used -10^6 to 10^6. The examples are ours.

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

arr holds distinct integers. For every two numbers in arr, take the absolute value of their difference, and let d be the smallest of these values. Return all pairs of numbers from arr whose difference is exactly d.

Write each pair as a list of two numbers with the smaller number first. Then sort the pairs in ascending order. Because the numbers are distinct, no two pairs start with the same number, so this simply sorts the pairs by their first number.

Function

minimumAbsDifference(arr: int[]) → List<List<Integer>>

Examples

Example 1

arr = [12,3,7,5,10]return = [[3,5],[5,7],[10,12]]

In sorted order the numbers are 3, 5, 7, 10, 12, and the differences between neighbours are 2, 2, 3 and 2. Two numbers that are not neighbours have at least one number between them, so their difference is the sum of two or more of these gaps, at least 4. The minimum difference is therefore 2, reached by [3,5], [5,7] and [10,12]. 5 is in two pairs, and the pairs are listed in ascending order.

Example 2

arr = [9,-9]return = [[-9,9]]

There is only one pair, so its difference, 18, is the minimum. The smaller number, -9, is written first, even though it comes second in arr.

Example 3

arr = [20,-3,17,0,-6,41]return = [[-6,-3],[-3,0],[17,20]]

In sorted order the numbers are -6, -3, 0, 17, 20, 41, and the differences between neighbours are 3, 3, 17, 3 and 21. Numbers that are not neighbours are further apart than any single gap between them, so the minimum difference is 3, reached by [-6,-3], [-3,0] and [17,20]. Negative numbers come first because the pairs are ordered by value, not by their position in arr.

Constraints

  • 2 <= arr.length <= 10^5
  • -10^6 <= arr[i] <= 10^6
  • All numbers in arr are distinct.

More Headlands Technologies interview questions

  • Minimum Changes for Distinct Adjacent CharactersOA · Seen Dec 2026
  • Sliding-Window Means with IEEE Special ValuesOA · Seen Dec 2026
  • Disaster RecoveryOA · Seen Nov 2026
  • Array Interval SumsOA · Seen Oct 2026
  • Job Runner Chain ReportsOA · Seen Oct 2026
  • As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
  • Exclusive Function Execution TimesOA · Seen Jan 2024
  • Earliest Dependency ScheduleOA · Seen Oct 2023
  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Market Order BookPHONE SCREEN · Seen Jun 2026

Similar questions at other companies

  • Macaron Order with Set DiscountsShopify · OAShopify interview questions
  • Build the Task HierarchyPendo · OA
  • Count User Mentions in Chat MessagesRamp · OARamp interview questions
  • Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Competition Winner and Runner-Up (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
  • Rank Football TeamsCapital One · OACapital One interview questions
  • Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
  • Rank Football TeamsPersona · OAPersona interview questions
  • Meandering ArrayVerisk · OA
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public List<List<Integer>> minimumAbsDifference(int[] arr) {
        // Write your code here.
    }
}
Checking account…