FastPrepInsert, Delete, And Get Random In Constant Time
Problems/Insert, Delete, And Get Random In Constant Time

Insert, Delete, And Get Random In Constant Time

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon full-time onsite interview. The report names the Insert Delete GetRandom O(1) question. Our function signature, examples, and limits are the same as in an equivalent version of this problem that we already reviewed. Our version is about 95% the same as the reported question.

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 not in the set and is now added, otherwise append "false".
  • For remove x, append "true" if x was 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, or getRandom.
  • -10^9 <= x <= 10^9.
  • Every getRandom occurs only when the set currently has size 1.

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)
See Amazon hiring insights
Saved
class Solution {
    public String[] processRandomizedSet(String[] operations) {
      // Write your code here.
    }
}
Checking account…