Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-228

Sparse Set Iteration Order

Problem

Simulate the dense array of a sparse set and report its iteration order.

operations contains "add" (record None), "remove" (record True or False), or "to_list" (record the current dense order). Removal swaps the removed value with the last dense element, which is exactly how a sparse set keeps O(1) deletes.

Return the list of recorded results.

Hint

Deleting swaps the removed value with the last dense element to keep the array packed.

Test cases

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

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