Contains Duplicate III
HardSliding Windowsliding window
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given an integer array nums and two non-negative limits index_diff and value_diff.
Return true if there exist two **different** indices i and j such that abs(i - j) <= index_diff and abs(nums[i] - nums[j]) <= value_diff. Otherwise return false.
Aim for O(n) expected time.
Examples
Input: nums = [1,2,3,1], index_diff = 3, value_diff = 0
Output: true
Indices 0 and 3 hold the same value and are 3 apart.
Input: nums = [1,5,9,1,5,9], index_diff = 2, value_diff = 3
Output: false
Equal values sit 3 indices apart, and any two values within 2 indices of each other differ by 4 or more.
Constraints
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- 1 <= index_diff <= nums.length
- 0 <= value_diff <= 10^9
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.