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

Bloomberg LP logoBloomberg LP● MediumNEW GRADINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in several firsthand Bloomberg new-grad phone and onsite interviews. In our version, insert, delete and random pick each take O(1) time on average, and every item is equally likely to be picked.

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 Bloomberg LP Phone Screen questions

  • N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
  • Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
  • Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
  • Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
  • Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
  • Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
  • Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions
  • Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
  • Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
  • Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
  • Linear-Probing Key-Value StoreEase · PHONE SCREEN
  • Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
  • Insert, Delete, And Get Random In Constant TimeAmazon · ONSITE INTERVIEW
  • Insert, Delete, And Get Random In Constant TimeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions

Interview reports that mention this question

  • Bloomberg SWE Intern Interview Questions: 68 Questions
  • Bloomberg Interview Questions, Level Not Reported: 65 Questions
  • Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
  • Bloomberg New Grad Question Bank: 161 Questions (Part 1 of 2)
  • Bloomberg New Grad Interviews: 2025 vs 2026 Guide
  • Bloomberg New Grad VOs: 32 Experiences (Part 5 of 5)
All Bloomberg LP interview questionsSee Bloomberg LP hiring insights
Saved
class Solution {
    public String[] processRandomizedSet(String[] operations) {
      // Write your code here.
    }
}
Checking account…