Reported September 2026
Goldman Sachsdesign

Insert, Delete, And Get Random In Constant Time

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

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

Up to 10^5 commands, and each insert, remove and getRandom has to run in average constant time. That rules out scanning a list, and it's the whole point of this Goldman Sachs OA question, reported in September 2026. It's the classic randomized set: a hash map plus a dynamic array. The twist here is the string-in, string-out wrapper, where you parse commands like "insert 1" and return "true", "false" or the lone value. If you blank on the swap-with-last trick, StealthCoder runs invisibly during the live assessment as a safety net. Know the shape before you sit down.

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 that stay in sync. Keep an array of values and a hash map from value to its index in the array. Insert: if the map has x, return false, otherwise push x and record its index. Remove: look up x's index, copy the last array element into that slot, update the moved element's index in the map, pop the array, and delete x from the map. That swap keeps removal O(1). The common pitfall is updating the map in the wrong order when x is the last element, which leaves a stale entry. Do the map write for the moved element first, then delete x. Another trap is parsing: split on the space and handle negative numbers. getRandom here only fires at size 1, but still write it as a random index into the array. StealthCoder is the hedge if the swap logic slips under the clock during the live OA.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

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

Goldman Sachs reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Insert, Delete, And Get Random In Constant Time FAQ

What's the trick in the Goldman Sachs randomized set problem?+

Pair a dynamic array with a hash map of value to index. The array gives O(1) random access, the map gives O(1) membership. For removal, swap the target with the last array element, pop the array, and fix the moved element's index in the map.

How hard is this one really?+

Medium on paper, easy once you've seen the swap-with-last move. The logic is short, about 25 lines. Most failures come from the edge case where you remove the last element, or from sloppy command parsing, not from the idea itself.

Why can't I just use a hash set?+

A hash set gives O(1) insert and remove, but you can't pick a uniformly random element in O(1). The tests only call getRandom on a single-value set, but the problem says the design should still work for larger sets, so use the array plus map.

What edge cases should I test before submitting?+

Remove a value that's missing, insert a duplicate, remove the only element then insert again, and remove the last element in the array. Also check negative numbers like insert -5, since parsing by splitting on whitespace must keep the sign.

How do I prepare for this in 48 hours?+

Write the structure from scratch twice without notes. Then write the command parser that maps each string to a method call and collects string results. Time yourself. If you can do it in under 15 minutes, you're ready for this pattern.

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

OA at Goldman Sachs?
Invisible during screen share
Get it