🐍 Python for Everyone

← Back to exercises

Chapter 25 Practice Bank 70-90 min, hands-on

Practice Bank: Memory Management in Python

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Standard library only — sys, gc, and tracemalloc, no installs needed.

How to run these Download starter.py below and run python3 starter.py from inside the same folder. Standard library only — nothing to install.

Topic 1: How Python Manages Memory & Reference Counting

1 Read a refcount

Find # TODO 1.1. Write current_refcount(obj).

2 Refcount goes up on alias

Find # TODO 1.2. Write refcount_increases_on_alias(obj).

3 Debug the Code

Find # TODO 1.3. This wrongly claims Python frees objects on a fixed daily schedule. Fix it.

💼 Scenario

Comparing Two Approaches

Find # TODO 1.A. Write will_object_survive(still_referenced).

💼 Interview Prep

The getrefcount() off-by-one

Find # TODO 1.B. Write explain_getrefcount_off_by_one().

Topic 2: Reference Cycles & the Garbage Collector

1 Build a cycle

Find # TODO 2.1. Write build_two_node_cycle().

2 Run the collector

Find # TODO 2.2. Write collect_garbage().

3 Debug the Code

Find # TODO 2.3. This hardcodes 1 generation instead of checking gc.get_count(). Fix it.

💼 Scenario

Comparing Two Approaches

Find # TODO 2.A. Write would_leak_without_gc(has_cycle, gc_enabled).

💼 Interview Prep

Why cycles need the cyclic GC

Find # TODO 2.B. Write explain_why_cycles_need_gc().

Topic 3: The Mutable Default Argument Gotcha

1 A safe append

Find # TODO 3.1. Write append_safe(item, items=None).

2 Same object reused

Find # TODO 3.2. Write same_default_object(func).

3 Debug the Code

Find # TODO 3.3. This has the classic mutable default bug in collect_tags(). Fix it.

💼 Scenario

Cleaning Up a Shared Utility

Find # TODO 3.A. Write make_empty_report().

💼 Interview Prep

The None-sentinel pattern

Find # TODO 3.B. Write explain_mutable_default_fix().

Topic 4: __slots__

1 Regular instance size

Find # TODO 4.1. Write regular_user_total_size(name, age).

2 Slotted instance size

Find # TODO 4.2. Write slotted_user_size(name, age).

3 Debug the Code

Find # TODO 4.3. This tests a RegularUser instead of a SlottedUser for the dynamic-attribute rejection. Fix it.

💼 Scenario

Choosing __slots__ for a Hot Path

Find # TODO 4.A. Write should_use_slots(instance_count, needs_dynamic_attrs).

💼 Interview Prep

What __slots__ costs

Find # TODO 4.B. Write explain_slots_tradeoff().

Topic 5: Memory Profiling Basics

1 Size of one object

Find # TODO 5.1. Write size_of_object(obj).

2 Traced peak of a build

Find # TODO 5.2. Write traced_peak_of(build_fn).

3 Debug the Code

Find # TODO 5.3. This returns the CURRENT traced memory where the PEAK was needed. Fix it.

💼 Scenario

Comparing Two Data Structures

Find # TODO 5.A. Write smaller_container(n).

💼 Interview Prep

getsizeof() vs. tracemalloc

Find # TODO 5.B. Write explain_getsizeof_vs_tracemalloc().

Topic 6: Generators as the Memory-Efficient Alternative

1 List version

Find # TODO 6.1. Write list_version(n).

2 Generator version

Find # TODO 6.2. Write generator_version(n).

3 Debug the Code

Find # TODO 6.3. This consumes the generator before taking the memory reading, so the peak is measured too late. Fix the order.

💼 Scenario

Streaming a Large Report

Find # TODO 6.A. Write should_use_generator(passes_needed, needs_random_access).

💼 Interview Prep

What a generator gives up

Find # TODO 6.B. Write explain_generator_memory_tradeoff().

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match on the explanation-style tasks — the goal is that your program runs without errors and does what each TODO asks. Every numeric task's output is exactly reproducible.
← Exercises Challenges →