Problem 204640 · medium · Phase 02 Linear Data Structures

Most Common Step

arrays · neighbours · counting

A conveyor sensor logs the position of a part at fixed intervals in positions. The step between two consecutive readings is positions[i] - positions[i - 1]. Return the step value that occurs most often; if several are tied, return the smallest one. Return None if there are fewer than two readings.

Examples

Input:  positions = [1, 3, 5, 6, 8, 10]
Output: 2
Explanation: Steps are 2, 2, 1, 2, 2.

Input:  positions = [5, 4, 6, 5, 7]
Output: -1
Explanation: Steps -1 and 2 both occur twice; -1 is smaller.

Input:  positions = [7]
Output: None

Constraints

  • 0 <= len(positions) <= 10**5
  • Target: O(n) time.

Goals

  • Aggregate a derived quantity (neighbour differences) instead of the raw values
  • Break ties deterministically
Starting Python…