Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-219

Sparse Set Add/Remove/Contains

Problem

Simulate a sparse set with O(1) membership, insertion, removal, and clearing.

operations contains "add" (record None), "remove" (record True when the value was present), "contains" (record a boolean), "size" (record the number of stored values), or "clear" (empty the set, recording None).

Return the list of recorded results.

Hint

The clear operation resets the whole set at once instead of removing values one by one.

Test cases

case 1
in: [["add","add","contains","remove","contains","clear","contains","size"],[5,3,5,5,5,0,5,0]]
expected: [null,null,true,true,false,null,false,0]
case 2
in: [["contains","remove"],[1,1]]
expected: [false,false]
case 3
in: [["add","clear","add","contains"],[2,0,2,2]]
expected: [null,null,null,true]
case 4
in: [["add","add","size","clear","size"],[1,2,0,0,0]]
expected: [null,null,2,null,0]
Starter codePython
def sparse_set_ops(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor