Insert, Delete, and Get Random in Constant Time
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
SambaNova Systems reportedly asked this one in March 2022, and the input size is the whole point. With up to 10^5 commands, scanning a list on every remove or insert gets you a timeout fast. The task is a randomized set: insert, remove, and getRandom, all in average O(1). Here the tests only call getRandom on a one-element set, so the output is deterministic, but the design still has to hold up. It's the classic hash map plus array pattern. If you freeze during the live assessment, StealthCoder runs invisibly on your screen as a safety net and hands you the structure.
The problem
Implement a set of integers that supports each of the following operations in average constant time: insert x adds x when it is absent and otherwise leaves the set unchanged. remove x deletes x when it is present and otherwise leaves the set unchanged. getRandom returns one uniformly random value from the current set. Process the commands in operations from left to right, starting from an empty set. Append one result for every command: For insert x, append "true" if x was absent and became inserted, otherwise append "false". For remove x, append "true" if x was present and became removed, otherwise append "false". For getRandom, append the unique remaining value as a decimal integer string. Every getRandom command is issued only when the set currently contains exactly one value, so the judged output is deterministic. Design the structure so that a later getRandom on a larger set would still be average O(1); the tests never ask for a stochastic sample from a multi-value set. Each command is exactly insert x, remove x, or getRandom, where x is a signed decimal integer. What the interview report shared The Superday report asked to design a data structure that can put, delete, and get a random element, each in O(1). Function processRandomizedSet(operations: String[]) → String[] Examples Example 1 operations = ["insert 1","insert 2","remove 1","getRandom"] return = ["true","true","true","2"] Inserting 1 and 2 both succeed. Removing 1 leaves only 2, so getRandom must return 2. Example 2 operations = ["insert 3","remove 4","insert 3","getRandom"] return = ["true","false","false","3"] The first insert creates singleton 3. Removing missing 4 and inserting 3 again both return false. The set still holds only 3. Constraints 1 <= operations.length <= 10^5. Each operation is exactly insert x, remove x, or getRandom. -10^9 <= x <= 10^9. Every getRandom occurs only when the set currently has size 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two structures working together. Keep a dynamic array of the values and a hash map from value to its index in that array. Insert checks the map, appends to the array, and records the index. Remove is the part people miss. Look up the index, copy the last array element into that slot, update the map for the moved element, pop the array, and delete the key from the map. That swap-with-last move is what keeps remove O(1) instead of O(n). The common pitfall is the order of operations when the removed value is itself the last element. Update the map before you delete, or you'll resurrect a stale key. getRandom returns array[random index]. Output strings must be exactly "true" or "false", and getRandom returns the value as a decimal string. If the swap logic goes blank mid-OA, StealthCoder is the hedge that covers you.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Insert, Delete, and Get Random in Constant Time 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
This OA pattern shows up on LeetCode as insert delete getrandom o1. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SambaNova Systems's OA.
SambaNova Systems 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.
Insert, Delete, and Get Random in Constant Time FAQ
What's the trick to this problem?+
Pair a dynamic array with a hash map from value to array index. Insert appends. Remove swaps the target with the last array element, pops, and fixes the map. That swap avoids shifting elements, which is why every operation stays average O(1).
How hard is this really?+
Medium. The idea is short once you've seen it, but the remove step has an edge case when the removed element is the last one. Most failures come from updating the map in the wrong order, not from the concept.
Why can't I just use a plain list or a set?+
A list makes remove O(n) because of the search and shift. A hash set gives O(1) insert and remove but no O(1) random access to an element. You need the array for random picks and the map for fast lookup.
Do I need real randomness for getRandom here?+
The tests only call getRandom when the set has exactly one value, so the result is deterministic. Still write it properly with a random index into the array, since the statement asks for a design that would work on larger sets.
How do I prepare for this in 48 hours?+
Write the structure from memory twice. Test it on the two examples, then on removing the last element and removing a missing value. Check that you return "true" and "false" as strings. Once the swap-with-last removal is automatic, you're done.