Find Objects Leaked by Reference Cycles in a Refcounted Heap
Last updated: November 29, 2026
Quick Overview
Given a heap snapshot of objects, their outgoing references, and the set of root references from stacks and globals, find objects that reference counting would never free but that are unreachable from any root.
0
0
0 solved
Given a heap snapshot of objects, their outgoing references, and the set of root references from stacks and globals, find objects that reference counting would never free but that are unreachable from any root.
Long-running Google services in Python, Objective-C or C++ with shared_ptr can leak memory through reference cycles, and engineers debug them from heap dumps. This onsite question tests graph traversal applied to memory management and an explanation of why refcounting alone misses cycles.
What the Interviewer Expects
- Explain why a cycle keeps refcounts above zero
- Compute reachability from roots with BFS or DFS
- Report unreachable objects that refcounting would keep alive
- Group leaked objects into cycles via strongly connected components
- Suggest fixes such as weak references
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How does CPython's cycle collector find garbage without knowing the roots?
- How would you handle a heap dump with 100 million objects?
- Which edge in a cycle should become weak?
- How would you prevent the leak from recurring in code review?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Input:
edgesas{obj: [referenced objs]}androotsas a list of objects referenced from outside the heap. - An object's refcount is the number of incoming heap edges plus root references.
- Ou...
High-Level Design
Step 1, reachability: BFS from roots gives the live set. Everything else is garbage by tracing semantics.
Step 2, simulate refcounting on the garbage: repeatedly free objects whose count is zero and ...