🐍 Python for Everyone

← Back to exercises

Chapter 26 Practice Bank (Categories 1-8) 90-120 min, hands-on

Practice Bank: Debugging Production Issues

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. Topics 1-5 cover Categories 1-2 (Crashes & Exceptions, Performance); Topics 6-10 cover Categories 3-8 (Memory Leaks, Concurrency & Deadlocks, Data Corruption, Deployment & Environment, Database & API Failures, and Logging & Observability). Standard library only — json, copy, math, collections — 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: Missing Keys & Out-of-Range Access

1 Raise on missing required config

Find # TODO 1.1. Write required_config_or_raise(config, required_keys).

2 A safe top-N sum

Find # TODO 1.2. Write safe_slice_sum(values, n).

3 Debug the Code

Find # TODO 1.3. This indexes from the front instead of slicing from the back. Fix it.

💼 Scenario

Comparing Two Approaches

Find # TODO 1.A. Write should_use_get(key_always_present).

💼 Interview Prep

dict[key] vs. dict.get()

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

Topic 2: Exception Handling That Hides Bugs

1 Catching a specific exception

Find # TODO 2.1. Write divide_or_none(a, b).

2 Catching only what you expect

Find # TODO 2.2. Write parse_int_or_default(raw, default).

3 Debug the Code

Find # TODO 2.3. This uses a bare except: that would hide unrelated bugs. Fix it.

💼 Scenario

Cleaning Up a Legacy Function

Find # TODO 2.A. Write explain_why_bare_except_is_risky().

💼 Interview Prep

Specific except vs. broad except

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

Topic 3: None, Zero & Other Quiet Failure Values

1 A lookup that returns None on no match

Find # TODO 3.1. Write find_or_none(items, predicate_key, predicate_value).

2 Handling that None safely

Find # TODO 3.2. Write get_email_safe(items, name).

3 Debug the Code

Find # TODO 3.3. This crashes with ZeroDivisionError on an empty list. Fix it.

💼 Scenario

Streaming API Data

Find # TODO 3.A. Write parse_api_results(raw_text).

💼 Interview Prep

None vs. raising an exception

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

Topic 4: Accidental O(n^2) Patterns

1 An O(1) membership check

Find # TODO 4.1. Write has_common_element(list_a, list_b).

2 Deduplicating with a set

Find # TODO 4.2. Write unique_in_order(items).

3 Debug the Code

Find # TODO 4.3. This uses list.insert(0, x) in a loop (O(n²)). Fix it with concatenation.

💼 Scenario

Comparing Two Approaches

Find # TODO 4.A. Write should_use_set_for_lookup(collection_size, checked_repeatedly).

💼 Interview Prep

The shape of an accidental O(n²) bug

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

Topic 5: Caching & Redundant Work

1 Caching repeated computations

Find # TODO 5.1. Write memoized_calls(keys, compute_fn).

2 Counting the "real" work needed

Find # TODO 5.2. Write count_distinct_calls(keys).

3 Debug the Code

Find # TODO 5.3. This recomputes an expensive lookup per item instead of caching by region. Fix it.

💼 Scenario

A Slow Report Generator

Find # TODO 5.A. Write should_cache_by_region(num_items, num_distinct_regions).

💼 Interview Prep

When caching is safe

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

Topic 6: Unbounded Growth & Shared State

1 Capping a growing list

Find # TODO 6.1. Write bounded_history(events, maxlen).

2 Evicting the oldest cache entry

Find # TODO 6.2. Write evict_oldest(cache, maxsize).

3 Debug the Code

Find # TODO 6.3. Logger uses a class-level history = [] shared by every instance. Fix it to be per-instance.

💼 Scenario

Tracking Without Owning

Find # TODO 6.A. Write should_use_weakref_registry(purpose_is_tracking_not_owning).

💼 Interview Prep

Reachable vs. leaked memory

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

Topic 7: Concurrency Shapes (Modeled Without Real Threads)

1 Modeling a lost update

Find # TODO 7.1. Write simulate_lost_updates(reads_before_writes).

2 Checking lock-order consistency

Find # TODO 7.2. Write acquisition_order_is_consistent(lock_orders).

3 Debug the Code

Find # TODO 7.3. pop_if_any() does a check-then-act on a shared list in two separate steps. Fix it to be a single atomic check-and-pop.

💼 Scenario

Threads vs. Processes

Find # TODO 7.A. Write should_use_multiprocessing(is_cpu_bound).

💼 Interview Prep

The GIL and threading

Find # TODO 7.B. Write explain_gil_and_threading().

Topic 8: Data Corruption & Precision

1 A real deep copy

Find # TODO 8.1. Write deep_independent_copy(original).

2 Comparing floats safely

Find # TODO 8.2. Write floats_close(a, b).

3 Debug the Code

Find # TODO 8.3. page_slice() has a pagination off-by-one that silently drops the first page. Fix the offset math.

💼 Scenario

The Mutable Default Argument Trap

Find # TODO 8.A. Write explain_mutable_default_argument_trap().

💼 Interview Prep

Shallow vs. deep copy

Find # TODO 8.B. Write explain_shallow_vs_deep_copy().

Topic 9: Deployment & Environment

1 Every missing env var at once

Find # TODO 9.1. Write missing_required_env(env, required_keys).

2 Reading from config, not a hardcoded value

Find # TODO 9.2. Write resolve_config_value(env, key, default).

3 Debug the Code

Find # TODO 9.3. startup_check() swallows a missing environment variable with a bare except:. Fix it to validate and raise a clear error naming what's missing.

💼 Scenario

"Works on My Machine"

Find # TODO 9.A. Write explain_works_on_my_machine().

💼 Interview Prep

Pinning dependencies

Find # TODO 9.B. Write explain_pinning_dependencies().

Topic 10: Database & API Reliability, and Observability

1 Avoiding N+1 lookups

Find # TODO 10.1. Write join_without_n_plus_1(records, lookup_table, key_name).

2 A safe idempotent retry

Find # TODO 10.2. Write charge_idempotently(processed, idempotency_key, amount).

3 Debug the Code

Find # TODO 10.3. summarize_log() builds a free-text log message instead of a structured dict. Fix it to return structured data a log aggregator could filter on.

💼 Scenario

Unknown vs. No

Find # TODO 10.A. Write explain_unknown_vs_no().

💼 Interview Prep

log.exception() vs. log.error(str(e))

Find # TODO 10.B. Write explain_log_exception_vs_str_e().

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.
← Exercises Challenges →