Minimum Absolute Difference
Updated
Problem statement
You are given a list of integers nums. Over all pairs of different positions i and j, find the smallest value of |nums[i] - nums[j]| and return it.
The list may contain repeated values; two equal values at different positions give a difference of 0. The answer can be larger than a signed 32-bit integer can hold, so return it as a 64-bit integer.
Function
minimumDifference(nums: int[]) → longExamples
Example 1
nums = [8,1,15,4]return = 3In increasing order the numbers are 1, 4, 8, 15, and the gaps between neighbours are 3, 4 and 7. The closest two numbers are 1 and 4, so the answer is 3.
Example 2
nums = [5,9,5]return = 0The value 5 appears twice, and two equal numbers differ by 0, so the answer is 0.
Example 3
nums = [-2147483648,2147483647]return = 4294967295The only pair differs by 2147483647 - (-2147483648) = 4,294,967,295. That is more than a signed 32-bit integer can hold, so compute it with 64-bit integers.
Constraints
2 <= nums.length <= 10^4-2^31 <= nums[i] <= 2^31 - 1
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Sort Products by Aisle With Frozen Items LastInstacart · ONSITE INTERVIEWInstacart interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Sort Documents Left to RightFigma · ONSITE INTERVIEWFigma interview questions
- Filter and Sort Scheduled TasksRippling · ONSITE INTERVIEWRippling interview questions