First Duplicate Value
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reported this one in September 2026, and the trap is the wrong definition of "first." It's not the duplicate value that appears earliest in the array. It's the one whose second occurrence comes earliest. In Example 1, 2 shows up first, but 3 wins because it repeats sooner. It's a hash-table problem with a single pass. Empty arrays and all-unique arrays both return -1. If you blank during the OA, StealthCoder runs invisibly as a safety net and reads the problem for you. Know the trick first, though, because it's about ten lines.
The problem
Scan values from left to right. Return the first value whose current occurrence has appeared earlier in the array. Equivalently, choose the duplicate whose second occurrence has the smallest index. Return -1 when no value occurs twice. Function firstDuplicate(values: int[]) → int Examples Example 1 values = [2,1,3,5,3,2] return = 3 The second occurrence of 3 appears before the second occurrence of 2. Example 2 values = [1,2,3,4] return = -1 Every value is unique. Constraints 0 <= values.length <= 200000. 0 <= values[i] <= 1000000000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the array left to right and keep a hash set of values you've seen. For each value, check the set. If it's already there, return it immediately, because that's the smallest second-occurrence index by construction. Otherwise add it and move on. If the loop ends, return -1. That's O(n) time and O(n) space, fine for 200000 elements. The common pitfall is counting frequencies first and then picking the earliest value with count above one. That returns 2 in Example 1, which is wrong. Another trap is sorting, which destroys the index order you need. Values go up to 1000000000, so a fixed-size array indexed by value won't work. Use a real set. Empty input should fall straight through to -1. If the live OA has you freezing on the definition, StealthCoder is the hedge that gets you the one-pass set solution.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill First Duplicate Value cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
First Duplicate Value FAQ
What's the trick in the Microsoft First Duplicate Value problem?+
Return on the first value you find already in a set. Scanning left to right guarantees that value has the earliest second occurrence. No counting pass, no sorting. Most wrong answers come from misreading "first" as the earliest first occurrence instead of the earliest second occurrence.
How hard is this one really?+
Easy once you read the definition correctly. The code is a loop and a set. The difficulty is the wording, since Example 1 returns 3 rather than 2. Trace that example by hand before you write anything, and the logic locks in.
Why can't I use a frequency map and pick the first value with count over one?+
Because it answers a different question. Counting then scanning by first occurrence returns 2 for [2,1,3,5,3,2], but the expected answer is 3. You need the smallest index of the second occurrence, which only a single pass with early return gives you.
Can I use an array of booleans instead of a hash set?+
Not safely. Values go up to 1000000000, so an array indexed by value would be huge. Use a hash set. Only consider a boolean array if you compress values first, which costs extra sorting for no real benefit here.
What edge cases should I test before submitting?+
Test an empty array, a single element, all unique values like [1,2,3,4], and adjacent duplicates like [5,5]. Also test the Example 1 shape, where an earlier-starting duplicate loses to a later one that repeats sooner. All unique and empty must return -1.