🐍 Python for Everyone

← Back to exercises

Chapter 21 Practice Bank 75-95 min, hands-on

Practice Bank: Working with Databases

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 "database" here is a FakeConnection/FakeCursor or FakeCollection class standing in for a real mysql-connector-python/pymongo object — no live database or installed driver is needed 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 Database Connection Is, and Why a Driver

1 Does this system need a driver?

Find # TODO 1.1. Write needs_a_driver(system).

2 The shared connect-run-read shape

Find # TODO 1.2. Write connection_steps().

3 Debug the Code

Find # TODO 1.3. This claims a driver stores the whole database in Python's memory. Fix it.

💼 Scenario

Choosing whether a driver is needed

Find # TODO 1.A. Write does_this_tool_need_a_driver(uses_csv_only).

💼 Interview Prep

Why Python needs a driver at all

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

Topic 2: Connecting to MySQL and Cursors

1 Getting a cursor

Find # TODO 2.1. Get a connection and cursor from FakeConnection.

2 A reusable query helper

Find # TODO 2.2. Write run_query(cursor, sql, params).

3 Counting matching rows

Find # TODO 2.3. Write count_matching_rows(cursor, sql, params).

4 Debug the Code

Find # TODO 2.4. This calls .execute() directly on a connection instead of a cursor. Fix it.

💼 Scenario

Reusable query helper

Find # TODO 2.A. Write fetch_students_above(cursor, threshold).

💼 Interview Prep

Cursor vs. connection

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

Topic 3: Parameterized Queries and SQL Injection

1 Building a parameterized query

Find # TODO 3.1. Write build_parameterized_query(column, value).

2 Spotting suspicious input

Find # TODO 3.2. Write looks_like_injection_attempt(user_input).

3 Debug the Code

Find # TODO 3.3. This builds a query with an f-string. Fix it to be parameterized.

💼 Scenario

Reviewing a teammate's query code

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

💼 Interview Prep

Why input-checking isn't enough

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

Topic 4: MongoDB and Documents

1 Documents with different shapes

Find # TODO 4.1. Insert two differently-shaped documents into one collection.

2 Checking for a field

Find # TODO 4.2. Write has_field(document, field_name).

3 Debug the Code

Find # TODO 4.3. This claims every document must share the same fields. Fix it.

💼 Scenario

Modeling a product catalog

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

💼 Interview Prep

The flexible-schema tradeoff

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

Topic 5: CRUD with pymongo

1 Insert and count

Find # TODO 5.1. Insert three documents and count them.

2 Updating a field

Find # TODO 5.2. Use update_one with {"$set": ...}.

3 Deleting a document

Find # TODO 5.3. Use delete_one and confirm the rest remain.

4 Debug the Code

Find # TODO 5.4. This update is missing the "$set" wrapper. Fix it.

💼 Scenario

A simple inventory tracker

Find # TODO 5.A. Write restock_item(collection, item_name, amount).

💼 Interview Prep

update_one vs. update_many

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

Topic 6: Relational vs. Document Data

1 Which model fits better?

Find # TODO 6.1. Write fits_relational_better(has_fixed_shape, needs_joins).

2 Describing a join

Find # TODO 6.2. Write describe_join().

3 Debug the Code

Find # TODO 6.3. This overstates what relational databases can never do. Fix it.

💼 Scenario

Choosing a model for a new feature

Find # TODO 6.A. Write recommend_model_for_user_profiles().

💼 Interview Prep

The 30-second comparison

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

Topic 7: Cloud-Hosted Connections and Environment Variables

1 Building an env-backed config

Find # TODO 7.1. Write build_env_backed_config(keys).

2 Spotting a hardcoded secret

Find # TODO 7.2. Write is_hardcoded_secret(line_of_code).

3 Debug the Code

Find # TODO 7.3. This hardcodes a password directly in the source. Fix it.

💼 Scenario

Reviewing a deployment config

Find # TODO 7.A. Write review_hardcoded_password().

💼 Interview Prep

What TLS adds

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

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 2's debug task intentionally demonstrates a real AttributeError (caught and printed) before it's fixed — that's the whole point of that task.
← Exercises Challenges →