Random Removal from a Set
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Bloomberg OA from July 2019 is the swap: pick index 2 in [1,4,7,9], grab 7, and 9 jumps into its slot. That's the whole problem. It's a simulation of the dense array trick behind O(1) random removal, the same idea used in insert-delete-getRandom designs. You get values and choices, and you return what each choice picked, in order. There's no tricky math and no search. It's easy to overthink and easy to botch the last-element edge case. If you blank during the live OA, StealthCoder sits invisibly on your screen as a safety net.
The problem
Initialize a dense set with the distinct integers in values. For each value in choices, treat it as a zero-based random index into the set's current dense array. Append the selected value to the result, then remove it in O(1) time by moving the current last value into its slot. Return the selected values in order. Function randomRemovalSequence(values: int[], choices: int[]) → int[] Examples Example 1 values = [1,4,7,9] choices = [2,0] return = [7,1] Removing index 2 selects 7 and swaps 9 into its slot; removing index 0 then selects 1. Constraints Values are distinct. choices.length <= values.length. Each choice is valid for the current size.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Copy the distinct values into an array. For each choice, read arr[choice] and push it to the result. Then overwrite arr[choice] with the last element and pop the last. That's O(1) per removal and O(n) total. The pitfall is order of operations. If the chosen index is the last one, overwriting then popping still works, but only if you read the selected value first. Don't use splice or remove-by-shift, since that makes each step O(n) and ignores the swap behavior the output depends on. The swap changes which value sits at each index, so a shift-based removal gives different answers on the next choice. Check Example 1: [1,4,7,9], choice 2 gives 7, array becomes [1,4,9]. Choice 0 gives 1. Result [7,1]. If you freeze on the OA, StealthCoder is the hedge that reads the statement and hands you this swap-and-pop loop.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Random Removal from a Set 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Random Removal from a Set FAQ
How hard is the Random Removal from a Set question really?+
It's easy. You simulate a swap-and-pop on an array, one step per choice. The only real difficulty is reading the statement carefully so you do the swap with the last element instead of shifting. Most candidates finish it in a few minutes once they see it.
What's the trick to this problem?+
Removal by swapping the last element into the vacated slot, then popping the end. Read the value at the chosen index first, store it in the result, then do the overwrite and pop. That keeps each removal O(1) and matches the expected output exactly.
Why can't I just use splice or remove by shifting?+
Shifting changes the order of the remaining elements differently than the swap does. In Example 1, removing index 2 puts 9 at index 2. A shift would leave the array as [1,4,9] too here, but with other inputs the results diverge. It's also O(n) per removal.
What edge cases should I test?+
Test choosing the last index, where the slot and the last element are the same. Test a single-element array. Test an empty choices list, which returns an empty result. The constraints guarantee each choice is valid for the current size, so you don't need bounds handling.
How do I prepare for this in 48 hours?+
Write the dense array with swap-and-pop from memory once, then trace Example 1 by hand. Also look at the insert, delete and getRandom design pattern, since it uses the same trick with a hash map for index lookups. That covers this question and its close relatives.