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.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

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
reference counting
garbage collection
graph reachability
strongly connected components
memory leaks
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements
  • Input: edges as {obj: [referenced objs]} and roots as 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 ...


Submit Your Answer
Markdown supported