Unique Reverse-Recency Browser History
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA from July 2022 looks like a freebie, and that's exactly where people trip. You get a list of URLs, oldest to newest, and return the history newest first with no duplicates. The catch is that a revisit has to move the URL to the front, not just get skipped. Most candidates grab a set and keep the first occurrence, then wonder why Example 1 fails. The hinted pattern is two pointers over a reversed scan, backed by a hash set. If you blank on the live assessment, StealthCoder runs invisibly as a safety net and hands you the clean version.
The problem
A browser receives the URLs in visits from oldest to newest. Return the final history from newest to oldest. Each URL must appear at most once. Revisiting a URL removes its earlier position and makes it the newest history entry. Function uniqueBrowserHistory(visits: String[]) → String[] Examples Example 1 visits = ["www.google.com","www.bing.com","www.facebook.com","www.google.com","www.bloomberg.com"] return = ["www.bloomberg.com","www.google.com","www.facebook.com","www.bing.com"] The second Google visit replaces its older position and becomes newer than Facebook and Bing. Example 2 visits = ["a","b","a","b"] return = ["b","a"] The last occurrence of each URL determines its recency. Constraints 0 <= visits.length <= 10^5. Each URL is a nonempty case-sensitive string.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: the last occurrence of each URL decides its position. So walk the array from the end to the start. Keep a hash set of URLs you've seen. If a URL isn't in the set, add it and append it to the result. If it is, skip it. The result comes out newest to oldest already, so you never reverse anything. That's O(n) time and O(n) space, which matters with up to 10^5 visits. The pitfall is the forward scan with a set, which keeps the first occurrence and gives the wrong order. Another trap is removing from a list on every revisit, which is O(n^2) and times out. Also handle the empty array, and remember URLs are case-sensitive, so don't lowercase anything. If your mind goes blank mid-assessment, StealthCoder is the hedge that reads the problem and gives you this reverse-scan solution live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Unique Reverse-Recency Browser History 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Unique Reverse-Recency Browser History FAQ
How hard is the Bloomberg unique browser history problem really?+
Easy once you see it. It's a single pass with a hash set. The difficulty is purely in picking the right direction. If you scan forward you'll fight the ordering. Scan backward and the answer builds itself in the required order.
What's the trick to getting the order right?+
Iterate from the last visit to the first. Add a URL to the result only the first time you see it in that backward scan. That first backward sighting is the latest visit, and the output is already newest to oldest, so no reversal is needed.
Why does the first-occurrence set approach fail?+
A forward scan with a set keeps the oldest visit of each URL. Example 2, [a,b,a,b], would give a,b instead of the expected b,a. The problem says the last occurrence sets recency, so the earliest one is the wrong one to keep.
What edge cases should I test before submitting?+
Test an empty array, a single URL, all identical URLs, and Example 2 where every URL repeats. Also check that case differences like Google versus google count as different URLs. With 10^5 entries, avoid any list removal inside a loop.
How do I prepare for this in 48 hours?+
Write the reverse-scan plus hash set solution from memory twice. Then do two or three similar dedupe-by-last-occurrence problems. This pattern is short, so the goal is reflex, not depth. Know the complexity and the empty-input case before you start.