Reported June 2022
Bloombergdesign

Insert Delete GetRandom O(1) - Duplicates Allowed

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reported this one in June 2022, and the catch is right in the title: average O(1) for insert, remove, and getRandom on a multiset with duplicates. Scanning a list for removal is the brute force, and it's exactly what the O(1) requirement kills. This is the classic randomized collection problem, built on a hash table plus an array. If you've got the OA invite and you're short on time, know the structure cold. StealthCoder sits invisibly on your screen as a safety net if the index bookkeeping slips away mid-assessment.

The problem

Process commands on a multiset supporting average O(1) insertion, removal, and uniform random sampling over stored occurrences.
insert x adds one occurrence and returns true exactly when x was previously absent.
remove x removes one occurrence and returns whether removal occurred.
getRandom returns a uniformly random stored occurrence.
Return one string result per command. For deterministic judging, every getRandom command occurs when all stored occurrences have the same value.

Function
processRandomizedCollection(operations: String[]) → String[]

Examples
Example 1
operations = ["insert 1","insert 1","insert 2","remove 1","remove 2","getRandom"]
return = ["true","false","true","true","true","1"]
One occurrence of 1 remains before getRandom.

Constraints
1 <= operations.length <= 10^4.
Every getRandom is issued on a nonempty collection whose distinct-value count is one.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two structures working together. Keep a dynamic array of every stored occurrence, and a hash map from value to a set of indices where that value sits in the array. Insert appends to the array, adds the index to the set, and returns true only if the set was empty before. Remove grabs any index of x, swaps the last array element into that slot, updates both sets, then pops the tail. The pitfall is the order of updates when the removed element is itself the last one. Do the set add and remove carefully or you'll corrupt the index. Here getRandom only fires when all stored values are identical, so you can return any element. Still, implement it properly. If the swap logic goes blank on you live, StealthCoder is the hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Insert Delete GetRandom O(1) - Duplicates Allowed 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as insert delete getrandom o1 duplicates allowed. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Insert Delete GetRandom O(1) - Duplicates Allowed FAQ

What's the trick to getting O(1) removal with duplicates?+

Swap the element you're removing with the last array element, then pop the tail. A map from value to a set of indices lets you find any occurrence of x instantly and fix the moved element's index after the swap.

How hard is this Bloomberg question really?+

It's a medium-hard design problem. The idea is simple, but the index updates are easy to get wrong, especially when the removed element is also the last one. Dry-run the example with two 1s before you submit.

Why does insert return true only sometimes?+

It returns true exactly when x wasn't present before. Check whether x's index set is empty or missing before you add the new index. Duplicates still get stored, but they return false.

Do I need real randomness for getRandom here?+

The statement says every getRandom happens when all stored occurrences share one value, so output is deterministic. Still, pick a random index from the array, since that matches the real uniform-sampling requirement and keeps your solution correct.

How do I prepare for this in 48 hours?+

Write the structure from scratch twice: array plus value-to-index-set map. Test the edge cases: removing the last element, removing a value with one occurrence, and removing something absent. That covers nearly every bug people hit.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it