🐍 Python for Everyone

← Back to exercises

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

Practice Bank: Multithreading

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. Every "wait" here uses time.sleep() to stand in for a slow network or disk operation — no real network access or file I/O happens anywhere in this bank. No import beyond json/os/re/math/datetime/random/csv/threading/concurrent.futures.

How to run these Download starter.py below and run python3 starter.py from inside the same folder.

Topic 1: What a Thread Is, and I/O-Bound vs. CPU-Bound

1 Classifying I/O-bound vs. CPU-bound

Find # TODO 1.1. Write is_io_bound(task_name).

2 Estimating sequential time

Find # TODO 1.2. Write estimate_sequential_time(task_seconds).

3 Estimating threaded time

Find # TODO 1.3. Write estimate_threaded_time(task_seconds).

4 Debug the Code

Find # TODO 1.4. This classifies a download as CPU-bound. Fix it.

💼 Scenario

Explaining why threading helps downloads

Find # TODO 1.A. Write explain_why_threading_helps_downloads().

💼 Interview Prep

I/O-bound vs. CPU-bound

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

Topic 2: Creating and Starting Threads

1 Threads appending to a shared list

Find # TODO 2.1. Write append_double(n, results_list).

2 Two named tasks

Find # TODO 2.2. Write slow_greet(name, seconds, results_list).

3 Counting started threads

Find # TODO 2.3. Write count_threads_started(n).

4 Debug the Code

Find # TODO 2.4. Threads are joined immediately after starting, removing all concurrency. Fix it.

💼 Scenario

Running any task concurrently

Find # TODO 2.A. Write run_tasks_concurrently(task_fn, items, results_list).

💼 Interview Prep

Start before join

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

Topic 3: The GIL

1 Which operations release the GIL

Find # TODO 3.1. Write gil_releases_during(operation).

2 Would threading help?

Find # TODO 3.2. Write would_threading_help(work_type).

3 Debug the Code

Find # TODO 3.3. This claims Python threads run fully in parallel on separate cores. Fix it.

💼 Scenario

Explaining the GIL to a teammate

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

💼 Interview Prep

The GIL's practical impact

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

Topic 4: Race Conditions

1 Spotting race-condition risk

Find # TODO 4.1. Write has_race_risk(shared, mutated_by_multiple_threads).

2 An unsafe shared counter

Find # TODO 4.2. Run unsafe_increment(times) across 4 threads.

3 The hidden steps of +=

Find # TODO 4.3. Write steps_in_plus_equals().

4 Debug the Code

Find # TODO 4.4. This claims the GIL fully prevents race conditions. Fix it.

💼 Scenario

Diagnosing a flaky counter bug

Find # TODO 4.A. Write diagnose_flaky_counter_bug().

💼 Interview Prep

Why race conditions are hard to catch

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

Topic 5: threading.Lock

1 Fixing the shared counter

Find # TODO 5.1. Write safe_increment(times) using lock_51.

2 Safely appending to a shared list

Find # TODO 5.2. Write append_safely(item, shared_list, lock_obj).

3 The with lock: pattern

Find # TODO 5.3. Write with_lock_pattern_steps().

4 Debug the Code

Find # TODO 5.4. A lock was created but never actually used. Fix it.

💼 Scenario

A thread-safe logger

Find # TODO 5.A. Write build_thread_safe_logger().

💼 Interview Prep

The tradeoff of using a lock

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

Topic 6: Thread Safety Patterns

1 Combining independent results

Find # TODO 6.1. Write compute_square(n, results_list).

2 threading.local()

Find # TODO 6.2. Write make_thread_local_counter().

3 Summing independent ranges

Find # TODO 6.3. Write sum_ranges_independently(ranges).

4 Debug the Code

Find # TODO 6.4. Harden a shared-dict write with a lock.

💼 Scenario

Parallel word count

Find # TODO 6.A. Write parallel_word_count(chunks).

💼 Interview Prep

Shared-nothing design vs. locking

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

Topic 7: ThreadPoolExecutor

1 .map() for ordered results

Find # TODO 7.1. Write double_val(n).

2 .submit() + as_completed()

Find # TODO 7.2. Write slow_square(n).

3 A reusable pool-running helper

Find # TODO 7.3. Write run_with_pool(fn, items, max_workers).

4 Debug the Code

Find # TODO 7.4. A pool was never used as a context manager. Fix it.

💼 Scenario

Fetching weather for several cities concurrently

Find # TODO 7.A. Write fetch_all_weather(cities).

💼 Interview Prep

Why ThreadPoolExecutor over raw Thread

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

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks. One exception: Topic 4's race-condition demo and Topic 5's "broken" debug case print a number that varies between runs by design — that's the whole point of those two tasks.
← Exercises Challenges →