The Minimum Consecutive Cards to Pick Up problem gives you an array representing a deck of cards. You want to pick up a consecutive sequence of cards from the deck such that at least one card appears twice in that sequence. You need to find the minimum length of such a sequence. If no card appears twice, return -1.
This is a standard Minimum Consecutive Cards interview question at Meta and Bloomberg. it tests Hash Tables and the "Sliding Window" concept (though it's more of a "Distance between duplicates" problem). It evaluates if you can keep track of the most recent position of each element to calculate distances efficiently.
The Hash Table / Last Position Tracking interview pattern is the optimal way to solve this in time.
current_index - last_index + 1.Cards: [3, 4, 2, 3, 4, 7].
3 at index 0. Map: {3: 0}.4 at index 1. Map: {3: 0, 4: 1}.2 at index 2. Map: {3: 0, 4: 1, 2: 2}.3 at index 3. Found 3 in Map! Distance = 3 - 0 + 1 = 4. Min = 4. Update Map: {3: 3, 4: 1, 2: 2}.4 at index 4. Found 4 in Map! Distance = 4 - 1 + 1 = 4. Min = 4.
Minimum length = 4.current - last instead of current - last + 1. For cards at index 0 and 3, the sequence length is 4 ([0, 1, 2, 3]).This problem is a variation of "Finding the closest pair of identical elements." Use a Hash Map to remember where you've been. This Hash Table interview pattern is a core building block for many complex string and array problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Erasure Value | Medium | Solve | |
| Count Complete Subarrays in an Array | Medium | Solve | |
| Maximum Sum of Distinct Subarrays With Length K | Medium | Solve | |
| Fruit Into Baskets | Medium | Solve | |
| Count the Number of Good Subarrays | Medium | Solve |