Ctrl K

Repeat Within Reach

Easy
A turnstile logs badge numbers ids in the order people pass through. It looks suspicious when the same badge is scanned twice too close together. Return True if there are two different positions i and j with ids[i] == ids[j] and |i - j| ≤ reach. Otherwise return False.
Example 1
ids=[7, 3, 9, 7], reach=3 → True

Badge 7 was scanned at positions 0 and 3: 3 apart, which is within reach.

Example 2
ids=[7, 3, 9, 7], reach=2 → False

Same two scans, but now only a distance of 2 is allowed.

Example 3
ids=[4, 4], reach=0 → False
Constraints
  • 1 ≤ len(ids) ≤ 10⁵
  • -10⁹ ≤ ids[i] ≤ 10⁹
  • 0 ≤ reach ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues One Apart EasyXOR of a Close Pair EasyTop-Frequency Window Sums EasyMoving Window Median HardNarrowest Range Touching Every List Hard
loading Python…
Input
ids=[7, 3, 9, 7], reach=3
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.