Sparse Set with Constant-Time Updates
Updated
Problem statement
Maintain a set of integers in [0,maxValue] using a dense array and a sparse position array.
[0,value]addsvalue; adding an existing value changes nothing.[1,value]removesvalue; removing a missing value changes nothing.[2,value]queries membership and produces one Boolean output.
Every operation must run in worst-case O(1) time. Return query results in operation order.
Function
processSparseSet(maxValue: int, operations: int[][]) → boolean[]Examples
Example 1
maxValue = 10operations = [[0,3],[0,7],[2,3],[1,3],[2,3],[2,7]]return = [true,false,true]Three and seven are added; removing three does not affect seven.
Example 2
maxValue = 2operations = [[0,1],[0,1],[2,1],[1,1],[1,1],[2,1]]return = [true,false]Duplicate add and remove operations are idempotent.
Example 3
maxValue = 0operations = []return = []No operations produce no query results.
Constraints
0 <= maxValue <= 1000000.0 <= operations.length <= 200000.- Every operation has exactly two integers and a value in
[0,maxValue]. - Operation type is
0,1, or2.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Asteroid CollisionSiemens · PHONE SCREEN
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions