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

SambaNova Systems logoSambaNova Systems● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand SambaNova new-grad phone-screen report dated March 7, 2022 describes the well-known Insert Delete GetRandom O(1) 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 answer is always the same. 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.

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 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.

More SambaNova Systems Phone Screen questions

  • Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Minimum Window SubstringPHONE SCREEN · Seen Jun 2022
  • Number of ProvincesPHONE SCREEN · Seen Jun 2022
  • Merge Three Sorted ArraysPHONE SCREEN · Seen Jun 2022
  • Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Parse Variable, Expression, and Application TreesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
  • Decode StringPHONE SCREEN · Seen Mar 2022
  • Ugly NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2021
  • Diagonal TraversePHONE SCREEN · Seen Nov 2020

Similar questions at other companies

  • 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
  • Role-Based Network Access ControlOkta · PHONE SCREENOkta interview questions
  • Track Package Receipt GapsAmazon · PHONE SCREEN
  • Transformer KV Cache OperationsNVIDIA · PHONE SCREENNVIDIA interview questions
All SambaNova Systems interview questionsSee SambaNova Systems hiring insights
Saved
class Solution {
    public String[] processRandomizedSet(String[] operations) {
      // Write your code here.
    }
}
Checking account…