K Most Recent Unique Request IDs
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The end of the array is the most recent request, and that one detail drives the whole Microsoft question reported in October 2026. You scan right to left, keep the first sighting of each ID, and stop at k distinct. It's a hash-table problem with a short loop, and it's easier than it looks. Candidates tend to overthink it or reverse the array the wrong way. If you blank on the live OA, StealthCoder runs invisibly on your desktop and gives you the solution in real time. Know the shape first, though, and you won't need it.
The problem
You are given an array of request IDs, requests, and an integer k. The end of requests represents the most recent request. A request ID may appear more than once. Scan requests from right to left and collect each distinct request ID the first time you encounter it. Stop after collecting k distinct IDs. Return the collected request IDs in order from most recent to least recent. Function getMostRecentUniqueRequests(requests: String[], k: int) → String[] Examples Example 1 requests = ["item1","item2","item3","item1","item3"] k = 3 return = ["item3","item1","item2"] Scanning from right to left first collects "item3" and then "item1". The next "item3" is skipped because it has already been collected. Collecting "item2" produces the required three distinct IDs. Constraints 1 <= k <= requests.length <= 10^5 requests contains at least k distinct request IDs. Every requests[i] consists only of lowercase English letters and digits.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single backward pass with a hash set. Start at the last index and move left. If the ID isn't in the set, add it to the set and append it to the result. If the set size hits k, break. Because you walk from most recent to least recent, the result already has the right order, so no reversal or sorting is needed. Time is O(n) worst case, space is O(k) for the set plus the output. The common pitfall is scanning left to right and keeping the last occurrence, which forces a reverse and extra bookkeeping. Another is forgetting the early stop, which is harmless for correctness but wasteful at 10^5 elements. Skip duplicates silently. The constraints guarantee at least k distinct IDs, so you don't need an edge case for running short. If the assessment clock gets tight and your head goes blank, StealthCoder is the hedge that hands you this loop.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill K Most Recent Unique Request IDs 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
K Most Recent Unique Request IDs FAQ
How hard is the K Most Recent Unique Request IDs question really?+
Easy. It's one pass with a set. The only real risk is direction. Scan from the end of the array, since the end is the most recent request. If you get that right, the output order comes out correct without any extra work.
What's the trick to solving it fast?+
Iterate from index n-1 down to 0. Keep a hash set of seen IDs. When you see a new ID, add it to the set and to the result list. Break once the result has k items. No sorting, no reversing, and it runs in linear time.
Why not scan left to right?+
You'd have to track the last occurrence of each ID, then sort or reverse to get most recent first. That's more code and more room for bugs. The right-to-left scan gives you the correct order directly, so it's the cleaner and safer choice.
Do I need to handle fewer than k distinct IDs?+
No. The constraints say requests contains at least k distinct IDs, so the loop will always collect k before it runs out. You can skip a fallback branch, though a bounds-safe loop doesn't hurt if you want to be defensive.
How do I prepare for this in 48 hours?+
Write the right-to-left set loop from memory twice in your language of choice. Then practice similar dedupe problems with hash sets and ordered output. Check how your language handles string hashing and list appends. That's enough, since the pattern is simple and the code is under ten lines.