Challenge - 5 Problems
Garbage Collection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of reference counting example
Consider a simple reference counting garbage collector. If an object has 0 references, it is collected. Given the following sequence:
1. Create object A
2. Create object B referencing A
3. Remove reference from B to A
4. Remove all references to B
How many objects remain after step 4?
1. Create object A
2. Create object B referencing A
3. Remove reference from B to A
4. Remove all references to B
How many objects remain after step 4?
Attempts:
2 left
💡 Hint
Think about how reference counts change when references are removed.
✗ Incorrect
When B removes its reference to A, A's reference count drops to 0, so A is collected. Then removing all references to B causes B's count to drop to 0, so B is also collected. No objects remain.
🧠 Conceptual
intermediate1:30remaining
Understanding mark-and-sweep phases
In a mark-and-sweep garbage collector, what is the main purpose of the 'mark' phase?
Attempts:
2 left
💡 Hint
Think about what the collector needs to know before freeing memory.
✗ Incorrect
The mark phase traverses all reachable objects and marks them so they are not collected in the sweep phase.
📋 Factual
advanced2:00remaining
Identify error in pseudo-code for reference counting
Given this pseudo-code for decrementing reference count:
``` function decrement_ref(obj): obj.ref_count -= 1 if obj.ref_count == 0 free(obj) ```
What is the main problem with this code?
``` function decrement_ref(obj): obj.ref_count -= 1 if obj.ref_count == 0 free(obj) ```
What is the main problem with this code?
Compiler Design
function decrement_ref(obj): obj.ref_count -= 1 if obj.ref_count == 0 free(obj)
Attempts:
2 left
💡 Hint
Look carefully at the syntax of the if statement.
✗ Incorrect
In many languages like Python, the if statement requires a colon at the end. Missing it causes a syntax error.
❓ optimization
advanced2:00remaining
Choosing a garbage collection strategy for cyclic references
Which garbage collection method can handle cyclic references without leaking memory?
Attempts:
2 left
💡 Hint
Think about which method can detect unreachable cycles.
✗ Incorrect
Reference counting cannot detect cycles because objects in a cycle keep each other's counts above zero. Mark-and-sweep can find unreachable cycles and collect them.
🔍 Analysis
expert2:30remaining
Debugging memory leak in reference counting
A program uses reference counting but has a memory leak. Which of these scenarios is the most likely cause?
Attempts:
2 left
💡 Hint
Think about what reference counting cannot detect.
✗ Incorrect
Reference counting cannot detect cycles where objects reference each other, so their counts never reach zero, causing leaks.