The Contains Duplicate II interview question adds a distance constraint to the duplicate detection. You need to 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.
This Contains Duplicate II coding problem is asked by companies like Meta and Apple to see if a candidate can implement a Sliding Window efficiently. It evaluates how you manage a dynamic collection of data (the window) while processing a stream of inputs.
This utilizes the Array, Hash Table, Sliding Window interview pattern.
Array: [1, 2, 3, 1], k = 3
Whenever a problem involves a constraint like "within distance k" or "in a window of size k," think about how to maintain a Set or Map that only contains the elements currently in that window.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Fruit Into Baskets | Medium | Solve | |
| Maximum Sum of Distinct Subarrays With Length K | Medium | Solve | |
| Maximum Erasure Value | Medium | Solve | |
| Minimum Consecutive Cards to Pick Up | Medium | Solve | |
| Count the Number of Good Subarrays | Medium | Solve |