Insert Delete GetRandom O(1)
A medium-tier problem at 55% community acceptance, tagged with Array, Hash Table, Math. Reported in interviews at Affirm and 45 others.
Insert Delete GetRandom O(1) is a medium-difficulty design problem that tests whether you can build a data structure supporting three operations all in constant time. Affirm, X, Okta, Yelp, and seven other companies ask it. The trick isn't obvious: an array alone can't delete in O(1), and a hash set can't sample randomly in O(1). You need to combine both and know exactly how to swap-and-pop. About half of candidates accept on first attempt. If you hit this in a live assessment and freeze on the randomization angle, StealthCoder surfaces a working solution invisible to the proctor.
Companies that ask "Insert Delete GetRandom O(1)"
Insert Delete GetRandom O(1) is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe core insight is pairing a hash map with a backing array. The hash map stores element-to-index mappings. To delete, you swap the target with the last element, update the hash map pointers, then shrink the array. Random selection becomes a single O(1) array index. Most candidates start with a hash set and realize too late that sampling is O(n) on a set. Others try a linked list and hit memory/indexing walls. The edge case is tracking indices correctly during swap, and ensuring the hash map reflects post-swap state. This is pure design pattern work: once you see the swap trick, it clicks. StealthCoder is your safety net if the pattern doesn't surface under time pressure in the assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Insert Delete GetRandom O(1) recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Insert Delete GetRandom O(1) interview FAQ
Why can't I just use a hash set and iterate to a random index?+
Because iterating to index k in a set is O(k), not O(1). You need O(1) random access, which only an array provides. Hash sets lose insertion order in most languages. Pairing a map with a backing array gets you both O(1) deletion and O(1) sampling.
What's the swap-and-pop trick exactly?+
To delete element x at index i, swap it with the last element (at index n-1), update the hash map so the last element now points to index i, then remove the last element from the array. This avoids shifting and keeps deletion O(1).
Is this still asked at Affirm, X, and Yelp?+
Yes. This problem appears in reports from 46 companies total, including Affirm, X, Yelp, and Okta. It's a classic design interview, especially for roles where you need to understand data structure trade-offs and avoid premature optimization.
What's the hardest part to get right in the interview?+
Tracking index pointers after the swap. When you move the last element to fill the deleted slot, you must update the hash map immediately so the moved element points to its new index. Off-by-one or stale pointers cause silent bugs.
How does this relate to the Array and Hash Table topics?+
It's a hybrid design problem. Array gives you O(1) random access and O(1) append/pop. Hash Table gives you O(1) lookup and deletion by key. The problem forces you to combine both optimally rather than pick one and accept O(n) somewhere.
Want the actual problem statement? View "Insert Delete GetRandom O(1)" on LeetCode →