All practice tasks

Coding (Algo)

Count Matching Measurement Gaps

Hash map · Medium

An analysis tool compares pairs of recorded measurements and counts those separated by a required absolute gap. Measurements may repeat; two equal values at different positions are still two observations. Each unordered pair must be counted once.

Count index pairs i < j whose values have exactly the requested absolute difference.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ nums: integer[], difference: integer }

Fields

  • nums is an array of signed integers in its original order. Equal values at different indexes are separate entries.
  • difference is a non-negative integer absolute gap.

Output

Return value

  • Return an integer pair count. Different index pairs count separately even when their values are equal.
  • A zero gap counts equal-value pairs; a position cannot be paired with itself.

Constraints

  • 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
  • All requested totals and counts fit exactly in a safe JSON integer.
  • 0 <= difference <= 2000000000.

Examples

Example 1

{
  "difference": 2,
  "nums": [
    1,
    3,
    1
  ]
}

Returns

2

Example 2

{
  "difference": 0,
  "nums": [
    4,
    4,
    4
  ]
}

Returns

3

Required complexity

O(n) expected time and O(n) space

What this task practices

Count Matching Measurement Gaps is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.