Asked at 12 companies
6 recent
- Meta
- tcs
- Bloomberg
- Amazon
- Microsoft
- Arista Networks
- Airbnb
- Netflix
- Accenture
+2 moreShow fewer
- Adobe
- Zoho
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Counting with a hash map · A set for “have I seen this?”
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · API rate limits: “100 requests per minute” is a sliding window: count what falls inside the last 60 seconds, drop what slides out, and reject the extra.
ProblemProblem statementGiven an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.ReadHide
Problem Statement
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
""" + PROV + r"""
Input / Output
Input: nums = [1,2,3,1], k = 3
Output: true
Input: nums = [1,0,1,1], k = 1
Output: true
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Remember the last-seen index — a duplicate only counts if the two copies sit within k of each other, so you never need the full history of a value, just where you saw it most recently. One pass with a map from value to latest index answers every check in O(1).
Approach
Brute Force — Check Every Pair Within k, O(n * k)
for i in range(n):
for j in range(i + 1, min(i + k + 1, n)):
if nums[i] == nums[j]: return True
return False
Correct, but it re-compares the same neighborhood over and over.
Optimized — Hash Map of Last-Seen Index, O(n)
last = {} # value -> latest index
for i, x in enumerate(nums):
if x in last and i - last[x] <= k:
return True
last[x] = i
return False
Example: [1,2,3,1], k = 3 — the second 1 is at index 3, the last-seen 1 was at index 0, and 3 - 0 = 3 <= 3, so true. In [1,2,3,1,2,3] with k = 2, every repeat is 3 apart — too far, so false.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Check pairs in k | O(n * k) | O(1) | Re-scans each value's neighborhood |
| Last-seen map | O(n) | O(n) | One lookup per number, only latest index kept |
Solution code
class SolutionBruteForce:
"""O(n * k) time | O(1) space — for each index, check the next k positions."""
def containsNearbyDuplicate(self, nums: list[int], k: int) -> bool:
n = len(nums)
for i in range(n):
for j in range(i + 1, min(i + k + 1, n)):
if nums[i] == nums[j]:
return True
return False
class Solution:
"""O(n) time | O(n) space — map each value to its latest index."""
def containsNearbyDuplicate(self, nums: list[int], k: int) -> bool:
last = {}
for i, x in enumerate(nums):
if x in last and i - last[x] <= k:
return True
last[x] = i
return FalseOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language