← Back to Knowledge Base

KNOWLEDGE / 04

Python and Coding Whiteboard

Python for QA, algorithmic thinking, and practical technical interview exercises.

Questions and practice

Open a question to see the answer, examples, and exercises.

Why is a mutable default argument dangerous?Junior

Answer

The default object is created when the function is defined and reused across calls.

Examples

  • A default list may retain an item from a previous function call.

Practice exercises

  1. Write a minimal failing example and fix it by using None.
How do is and == differ in Python?Junior

Answer

The == operator compares values through the equality protocol, while is checks whether two names refer to the same object. is is appropriate for singleton values such as None; using it for strings or numbers is unsafe because of interning details.

Examples

  • value is None is a correct identity check; value == expected compares content.

Practice exercises

  1. Write an example where two separate lists are equal with == but is returns False, and explain why.
Why use a Python context manager?Junior

Answer

A context manager defines entry into a controlled context and guaranteed exit, even when an exception occurs inside it. It suits files, locks, transactions, temporary resources, and test fixtures because setup and cleanup stay together.

Examples

  • with open(...) closes a file automatically; a custom context manager can roll back a test transaction.

Practice exercises

  1. Create a context manager for a temporary environment variable that always restores the previous value.
How does a generator differ from a list?Middle

Answer

A generator yields values gradually; a list stores every item. A generator is usually single-use and has no arbitrary index access.

Examples

  • A large file can be streamed with a generator without loading every row into memory.

Practice exercises

  1. Implement batched data reading as a generator and compare memory use with a list.
What do type hints provide in dynamic Python?Middle

Answer

Type hints document expected contracts and let a static checker catch some errors before runtime. They do not automatically validate values at runtime and do not replace tests. Their greatest value appears at module boundaries, in complex data structures, and during refactoring.

Examples

  • A Protocol describes the behaviour required from an API client without coupling code to one concrete class.

Practice exercises

  1. Add typing to an API-response normalizer and handle Optional and union values explicitly.
How do you test exception paths without hiding errors?Middle

Answer

A test should trigger a specific condition, assert the exception type and meaningful attributes, and verify state after failure. A broad except or empty catch hides defects. For retryable failures, separately verify attempt count, backoff, and that non-retryable exceptions are not repeated.

Examples

  • pytest.raises checks ValidationError and its invalid_field attribute, while a fixture confirms that no database row was created.

Practice exercises

  1. Test a function for timeout, validation error, and unexpected error, defining a different policy for each.
Does asyncio speed up CPU-heavy code?Senior

Answer

Not by itself. It helps with concurrent I/O waiting; CPU-heavy work needs a different execution strategy.

Examples

  • Waiting for HTTP responses can benefit from asyncio, while heavy hash calculation blocks the event loop.

Practice exercises

  1. Classify five tasks as I/O-bound or CPU-bound and propose an execution model.
Why are cancellation and timeout part of async-code design?Senior

Answer

An async task can be cancelled at any await, so code must release resources correctly and avoid leaving partially changed state. A timeout bounds waiting but does not always stop the external operation. Structured concurrency helps tie child-task lifecycles to their parent operation.

Examples

  • After an HTTP timeout, a context manager closes the session, and unfinished child tasks are cancelled and awaited.

Practice exercises

  1. Design an async fan-out to three services with a shared deadline, partial results, and cleanup.
How should complexity be assessed in a coding whiteboard or code review?Senior

Answer

Big-O describes how cost grows with input, but practical review also considers data size, memory, I/O, readability, and profiling. A strong answer states assumptions and trade-offs instead of merely naming O(n). Optimising without measurement may complicate code without benefit.

Examples

  • Replacing nested list lookup with a set reduces expected time from O(n²) to O(n) while using additional memory.

Practice exercises

  1. Compare two duplicate-detection approaches and justify the choice for 100 and 10 million elements.