Detect a Bounded-Index Pair with Near-Equal Values

Quick Overview

Detect near-equal values within a bounded index window using exact ordered interval lookup and multiset expiration.

Detect a Bounded-Index Pair with Near-Equal Values

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Return whether there are distinct indices i,j satisfying both `abs(i-j) <= indexDiff` and `abs(nums[i]-nums[j]) <= valueDiff`. Either equality or near equality of values is allowed within the stated tolerance. ```cpp bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff); ``` ### Examples ```text Input: nums = [4,1,8,5], indexDiff = 3, valueDiff = 1 Output: true ``` Positions 0 and 3 qualify. ```text Input: nums = [-5,0,5], indexDiff = 1, valueDiff = 4 Output: false ``` ### Constraints - Length is between 2 and 100000. - Values lie between -1000000000 and 1000000000. - `1 <= indexDiff <= nums.length`. - `0 <= valueDiff <= 1000000000`.

Overview: Detect near-equal values within a bounded index window using exact ordered interval lookup and multiset expiration.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return whether there are distinct indices i,j satisfying both abs(i-j) <= indexDiff and abs(nums[i]-nums[j]) <= valueDiff. Either equality or near equality of values is allowed within the stated tolerance.

bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff);

Examples

Input: nums = [4,1,8,5], indexDiff = 3, valueDiff = 1
Output: true

Positions 0 and 3 qualify.

Input: nums = [-5,0,5], indexDiff = 1, valueDiff = 4
Output: false

Constraints

  • Length is between 2 and 100000.
  • Values lie between -1000000000 and 1000000000.
  • 1 <= indexDiff <= nums.length .
  • 0 <= valueDiff <= 1000000000 .