Interview Questions& Model Answers
Real questions. Real answers. Built from 20 years of actual hiring and being hired.
A decorator is a function that wraps another function to add behavior. Without functools.wraps the wrapper loses the original function's metadata like __name__ and __doc__.
Decorators work by taking a function as input and returning a new function that adds behavior before or after the original call. The syntax @decorator is syntactic sugar for function = decorator(function). The core problem is that the returned wrapper function has its own identity — its __name__ is 'wrapper' not the original function's name. This breaks logging debugging and documentation tools. functools.wraps(original_func) applied to the wrapper copies the original function's metadata to the wrapper. This is especially critical in Flask and FastAPI where the routing system uses function names to identify view functions — without wraps all decorated routes have the same name and only one will be registered.
In a Flask application a custom authentication decorator without functools.wraps caused all protected routes to map to the same endpoint name 'wrapper' making url_for() return wrong URLs and breaking the entire navigation system. Adding @functools.wraps(f) to the inner wrapper function fixed it immediately.
Forgetting @functools.wraps on the inner wrapper function. Decorators that do not preserve the function signature breaking tools that inspect function parameters. Applying decorators in the wrong order when stacking multiple decorators.
A production Flask API broke its authentication after a refactor added a logging decorator without functools.wraps. The route registration system saw multiple routes all named 'wrapper' and silently dropped all but one making several API endpoints return 404 despite the code being correct.
FastAPI uses Python type hints to automatically generate API validation serialization and OpenAPI documentation. Production-ready additions include async database access dependency injection for auth middleware for logging/CORS rate limiting and health check endpoints.
FastAPI is built on Starlette (ASGI framework) and Pydantic (data validation). You define endpoints as async functions with type-annotated parameters — FastAPI automatically validates inputs returns 422 for invalid data and generates Swagger UI documentation. Pydantic models define request/response schemas with validation. Dependency injection (Depends()) handles shared logic: database sessions authentication rate limiting. For production: use async ORMs (SQLAlchemy async Tortoise ORM) add middleware (CORS request logging timing) implement proper error handling with custom exception handlers add health check endpoints for load balancer probes use environment-based configuration (pydantic-settings) and containerize with uvicorn behind nginx.
A production API for a fintech app: Pydantic models validate all financial amounts (positive correct decimal places) JWT authentication is injected via Depends() into protected routes a PostgreSQL database is accessed via async SQLAlchemy Prometheus middleware exports metrics and a /health endpoint returns database connectivity status for the load balancer.
Using synchronous database drivers with async FastAPI (blocks the event loop destroying performance). Not validating response models (can leak internal data). Forgetting to handle the database connection lifecycle — connections not closed properly exhaust the pool. Not implementing proper HTTP status codes — returning 200 for errors.
A FastAPI service handling 500 req/s was experiencing periodic slowdowns. Investigation revealed synchronous calls to a third-party API inside async route handlers were blocking the event loop during each slow response. Replacing with httpx (async HTTP client) and proper timeout handling eliminated the slowdowns.
Type hints are annotations that specify expected types for variables function parameters and return values. They are ignored at runtime by default but used by static analysis tools (mypy pyright). Runtime enforcement requires libraries like Pydantic or beartype.
Python's type system is gradual — you add hints progressively without breaking existing code. Basic syntax: def greet(name: str) -> str. Complex types: List[str] Dict[str int] Optional[str] (can be None) Union[int str] and in Python 3.10+ int | str. Generic types allow parameterized classes: class Stack(Generic[T]). TypeVar creates generic type variables. Protocol defines structural subtyping (duck typing with type safety). At runtime type hints are stored in __annotations__ and are just metadata — Python does not check them. mypy and pyright perform static analysis. Pydantic validates at runtime using type hints for data parsing and validation. beartype provides runtime type checking with minimal overhead.
FastAPI's entire API surface is type-annotated — function parameter types define API request validation response model types define OpenAPI documentation and return type serialization. SQLAlchemy 2.0 uses type annotations for ORM model definitions. Both use the same type hints for static analysis AND runtime behavior.
Adding type hints to existing code and then being confused when it still fails at runtime (hints are not enforced by default). Using complex Union types when Optional (Union[X None]) is the common case. Not using TypedDict for dict structures with known keys (makes static analysis much more useful). Mixing legacy typing module types (List Dict) with modern built-in generics (list dict) available from Python 3.9+.
A production data pipeline was passing incorrectly typed arguments silently for months because no type checking was in place. Adding mypy to the CI pipeline immediately surfaced 47 type errors. Fixing them prevented a class of bugs that had been causing occasional data corruption. Three of the errors would have caused production failures in the next quarter based on upcoming data changes.
The Global Interpreter Lock (GIL) is a mutex that prevents multiple native threads from executing Python bytecode simultaneously. It makes Python threads unsuitable for CPU-bound parallelism.
CPython (the standard Python implementation) uses reference counting for memory management. The GIL protects this reference counting from race conditions by ensuring only one thread executes Python code at a time. This means Python threads do NOT run in true parallel for CPU-bound tasks — they take turns. However the GIL is released during I/O operations (file reads network calls database queries) so threading IS effective for I/O-bound tasks. For true CPU parallelism use the multiprocessing module which spawns separate processes each with their own GIL or use libraries like NumPy that release the GIL in their C extensions.
A web scraper using threading to fetch 100 URLs runs significantly faster with threads because most time is spent waiting for network I/O (GIL released). The same approach for parsing and processing 100 large JSON files (CPU-bound) would see no speedup from threading — multiprocessing or concurrent.futures ProcessPoolExecutor should be used instead.
Using threading for CPU-intensive tasks and being confused when there is no performance improvement. Assuming multiprocessing will always be better — it has high overhead for process spawning and IPC. Not considering asyncio for I/O-bound tasks which is more efficient than threading for high-concurrency scenarios.
A production image processing service used Python threading expecting parallel image resizing. Performance was identical to single-threaded execution. The fix was switching to multiprocessing.Pool which reduced processing time by 75% on an 8-core server by actually utilizing all cores.
Threading is for I/O-bound tasks with moderate concurrency. Asyncio is for I/O-bound tasks with high concurrency and fine-grained control. Multiprocessing is for CPU-bound tasks requiring true parallelism. The GIL makes threading unsuitable for CPU parallelism.
Threading: OS threads preemptive scheduling GIL limits CPU parallelism good for I/O-bound work where threads sleep during I/O (GIL released) moderate overhead race conditions possible. Asyncio: single-threaded cooperative concurrency a single thread switches between coroutines when they await I/O handles thousands of concurrent connections efficiently requires async/await syntax throughout (async code cannot call sync code without blocking the event loop) best for high-concurrency I/O (web servers API clients). Multiprocessing: separate OS processes each with own Python interpreter and memory true CPU parallelism high overhead (process creation IPC) no shared memory by default best for CPU-bound tasks (numerical computation image processing ML inference). Decision: high-concurrency I/O → asyncio. CPU parallelism → multiprocessing. Simple I/O parallelism with existing sync code → threading.
FastAPI uses asyncio for handling thousands of concurrent HTTP connections efficiently. A background task that processes images uses multiprocessing.Pool to distribute work across CPU cores. A legacy synchronous database library is called from a thread pool using asyncio's run_in_executor to avoid blocking the event loop.
Mixing asyncio and synchronous blocking calls — calling requests.get() in an async function blocks the entire event loop. Using multiprocessing for I/O-bound tasks (huge overhead for no benefit over threading). Using threading for CPU-bound tasks and wondering why there is no speedup. Not using asyncio.gather() for concurrent async operations calling them sequentially instead.
A FastAPI service was timing out under load despite appearing to handle requests correctly in development. Profiling revealed synchronous database calls (using the requests library instead of httpx) inside async route handlers blocking the event loop during every database query. Replacing with async database drivers (asyncpg databases library) resolved the timeouts.
The most practically useful Python patterns are: Singleton (via module-level objects or metaclass) Factory (via functions not classes) Strategy (via first-class functions) Observer (via callbacks or event systems) and Decorator (using Python's native decorator syntax). Python's first-class functions make many GoF patterns simpler or unnecessary.
Python's features change how classic patterns are implemented. Singleton: in Java you implement a private constructor with a static instance. In Python a module-level instance is already a singleton — module state is shared across all imports. Factory Method: in Java a separate factory class. In Python a function or callable that returns the right type is sufficient — first-class functions eliminate the need for a factory class hierarchy. Strategy: in Java each strategy is a class implementing an interface. In Python pass the strategy function directly — no class needed. Decorator: Python has native decorator syntax making this pattern trivially implementable. Observer/Event: Python's callable objects and collections of callbacks implement this cleanly without interface boilerplate. The key insight: Python's dynamic typing first-class functions and duck typing make many patterns simpler and reduce the class hierarchy complexity required in statically typed languages.
Django's middleware system is a chain-of-responsibility pattern implemented as callable objects. Flask's signal system (blinker) is an Observer pattern. SQLAlchemy's session uses Unit of Work pattern. Python's built-in sorted() function's key parameter is a Strategy pattern using first-class functions — sorted(users key=lambda u: u.last_name) passes the sorting strategy as a function.
Implementing Java-style patterns verbatim in Python (creating unnecessary class hierarchies). Not leveraging Python's first-class functions to simplify Strategy Command and Factory patterns. Implementing Singleton as a class when a module-level instance or functools.lru_cache(maxsize=None) serves the same purpose more simply.
A Python service implemented a complex Factory class hierarchy (AbstractFactory ConcreteFactory AbstractProduct ConcreteProduct) in Java style. Code review replaced it with a registry dictionary mapping string keys to constructor functions — 5 lines instead of 50 with identical functionality and better extensibility.
A metaclass is the class of a class — it controls how classes themselves are created. Use them when you need to enforce constraints auto-register classes or modify class definitions at creation time.
In Python everything is an object including classes. The default metaclass is 'type'. When Python processes a class definition it calls the metaclass to build the class object. By creating a custom metaclass (inheriting from type and overriding __new__ or __init__) you can intercept class creation and modify or validate it. Practical uses include: enforcing that all subclasses implement certain methods automatically registering plugin classes in a registry adding logging to all methods automatically and implementing singleton patterns. Django's ORM uses metaclasses to convert class-level field declarations into actual database schema mappings.
Django's Model metaclass (ModelBase) reads the field attributes you declare on a model class and builds the database schema query interface and validation logic automatically. Without metaclasses Django's ORM syntax would require explicit registration calls for every model field.
Overusing metaclasses for problems that class decorators or __init_subclass__ solve more simply. Metaclasses from different libraries conflicting when a class inherits from both (metaclass conflict). Writing metaclasses that are so abstract they become impossible to debug.
An internal plugin system at a SaaS company used a metaclass to automatically register all subclasses of a BasePlugin class into a global plugin registry. This eliminated the need for manual plugin registration and prevented the recurring production bug where developers created a plugin class but forgot to register it.
Python uses reference counting as the primary memory management mechanism supplemented by a cyclic garbage collector to handle reference cycles. Memory is allocated from private heaps managed by the Python memory manager.
Every Python object has a reference count. When you assign a variable or pass an object to a function the count increases. When a reference goes out of scope or is deleted the count decreases. When the count reaches zero memory is freed immediately. The problem is reference cycles: object A references B B references A — neither count reaches zero. Python's gc module handles this with a generational garbage collector that periodically identifies and clears cycles. Objects are sorted into three generations based on survival — most objects die young (generation 0) so the GC focuses there. You can trigger collection manually with gc.collect() and disable it in performance-critical code if you are certain there are no cycles.
A long-running FastAPI service was growing in memory over days. Profiling with tracemalloc revealed a reference cycle in a caching layer where cached response objects held references back to the cache container. Explicitly breaking the cycle with weakref.ref() eliminated the memory growth.
Assuming memory is freed immediately after del (del only removes the reference the GC frees memory). Creating reference cycles in data structures without using weakref. Disabling the GC for performance without understanding the cycle risk. Not using __slots__ in high-volume object creation wasting memory on per-instance __dict__.
A Python-based IoT data collector crashed with OOM after running for several days. Memory profiling showed 50000 DataPoint objects that should have been freed were kept alive by a reference cycle between DataPoint and its parent DataStream. Using weakref.ref for the back-reference fixed the leak.
PAGE 2 OF 2 · 23 QUESTIONS TOTAL