Maximum Gap

HardArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums, imagine it sorted in ascending order and return the largest difference between two neighbouring elements of that sorted order. If nums has fewer than two elements, return 0. The target is O(n) time and O(n) extra space, which rules out comparison sorting.

Examples

Input: nums = [3,6,9,1]
Output: 3
Sorted, the array is [1,3,6,9]. The gaps are 2, 3 and 3, so the answer is 3.
Input: nums = [10]
Output: 0
A single element has no neighbour, so the answer is 0.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems