Code RoomMaximum gap linear time
HardPrep Room Coding #1227

Maximum gap linear time

CodingAlgorithms & data structuresSenior–Staff~30 min

Given an unsorted integer array `nums`, return the maximum difference between two successive elements once the array is sorted. If the array has fewer than two elements, return 0. You must run in linear time and use linear extra space — so use bucket sort / the pigeonhole principle (the max gap is at least ceil((max-min)/(n-1)), so it must occur across bucket boundaries), NOT a comparison sort.

Implement
maximum_gap(nums: list[int]) → int
Examples
in[[3,6,9,1]]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[[3,6,9,1]]3not run yetsample