Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-205

GC Sweep Phase Count

Problem

Run mark and sweep and report the objects that are collected.

objects is the complete list of object ids, roots is the list of roots, and edges is a list of [from, to] references. Mark everything reachable from the roots, then sweep (collect) every other object.

Return the collected ids sorted in ascending order.

Hint

Anything not marked reachable is swept.

Test cases

case 1
in: [[1,2,3,4],[1],[[1,2],[2,3]]]
expected: [4]
case 2
in: [[1,2],[],[[1,2]]]
expected: [1,2]
case 3
in: [[],[],[]]
expected: []
case 4
in: [[1,2,3],[1,2,3],[]]
expected: []
case 5
in: [[1,2,3],[1],[[2,3]]]
expected: [2,3]
Starter codePython
def gc_collect(objects, roots, edges):
    # Your code here
    pass

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