Return all integer pairs with the smallest absolute difference
Company: Headlands
Role: Quantitative Researcher
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Overview: Find every pair of distinct integers that attains the minimum absolute difference. Return smaller values first and pairs in canonical order while handling ties and negative inputs.
Read the full Headlands Quantitative Researcher interview experience this question came from
Constraints
- `2 <= len(values) <= 100000`. All values are distinct.
- For this practice version, values are between -1000000000 and 1000000000, so differences fit in signed 32-bit integers.
Examples
Input: ([4, 1, 3, 2],)
Expected Output: [[1, 2], [2, 3], [3, 4]]
Explanation: Return every tied adjacent pair in ascending order.
Input: ([-5, 9],)
Expected Output: [[-5, 9]]
Explanation: A two-element input has exactly one possible pair.
Hints
- Consider what happens to a gap if another value lies strictly between its endpoints.
- Sorting the values can also give the required order for the resulting pairs.