Insert, Delete, And Get Random In Constant Time
Updated
Problem statement
Implement a set of integers that supports each of the following operations in average constant time:
insert xaddsxwhen it is absent and otherwise leaves the set unchanged.remove xdeletesxwhen it is present and otherwise leaves the set unchanged.getRandomreturns 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"ifxwas not in the set and is now added, otherwise append"false". - For
remove x, append"true"ifxwas in the set and is now removed, otherwise append"false". - For
getRandom, append the one value left in the set, written as text.
Every getRandom command comes only when the set has exactly one value, so the answer is always known. Still build the structure so that getRandom on a larger set would be average O(1). The tests never ask for a random pick from a set with more than one value.
Each command is exactly insert x, remove x, or getRandom, where x is a signed decimal integer.
Requirements
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 adds 3 to the empty set. 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, orgetRandom. -10^9 <= x <= 10^9.- Every
getRandomoccurs only when the set currently has size1.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Insert, Delete, And Get Random In Constant TimeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
- Array With Set AllMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Insert, Delete, and Get Random in Constant TimeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEWOkta interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)