Skip to main content
Home  /  Knowledge Hub  /  Interview Questions

Interview Questions& Model Answers

Real questions. Real answers. Built from 20 years of actual hiring and being hired.

54
Total Questions
3
Technologies
3
Levels
✕ Clear filters

Showing 23 questions · Python

Clear all filters
PY-INT-002 How do decorators work in Python and what is the functools.wraps issue?
Python Core Python Intermediate
5/10
Answer

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__.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
How do class-based decorators work? How do you write a decorator that accepts its own arguments? How does decorator stacking (applying multiple decorators) work in Python??
ID: PY-INT-002  ·  Difficulty: 5/10  ·  Level: Intermediate
PY-INT-007 How do you build a REST API with FastAPI and what makes it production-ready?
Python Data Science Intermediate
6/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
What is ASGI vs WSGI? How does Pydantic validation work under the hood? What is the difference between FastAPI and Flask for production APIs??
ID: PY-INT-007  ·  Difficulty: 6/10  ·  Level: Intermediate
PY-ADV-004 What are Python type hints and how do they work with runtime type checking?
Python Core Python Advanced
6/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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+.

🏭 Production Scenario

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.

Follow-up Questions
What is the difference between mypy and pyright? What is TypedDict and when is it better than a dataclass? What is Protocol and how does it differ from ABC??
ID: PY-ADV-004  ·  Difficulty: 6/10  ·  Level: Advanced
PY-INT-003 What is the GIL in Python and how does it affect multithreading?
Python Performance Intermediate
6/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
What is the difference between threading multiprocessing and asyncio? When does Python release the GIL? Does Jython or PyPy have a GIL??
ID: PY-INT-003  ·  Difficulty: 6/10  ·  Level: Intermediate
PY-ADV-003 What is the difference between multiprocessing threading and asyncio in Python — and how do you choose?
Python Performance Advanced
7/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
What is the event loop in asyncio and how does it work? What is run_in_executor and when should you use it? How does uvicorn serve FastAPI using asyncio??
ID: PY-ADV-003  ·  Difficulty: 7/10  ·  Level: Advanced
PY-ADV-005 What are the most important design patterns in Python and how do they differ from Java implementations?
Python Core Python Advanced
7/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
What is the difference between a class decorator and a function decorator? How do you implement an event system in Python? What is the Repository pattern and how does it apply to Python ORMs??
ID: PY-ADV-005  ·  Difficulty: 7/10  ·  Level: Advanced
PY-ADV-001 What is a metaclass in Python and when would you actually use one?
Python Core Python Advanced
8/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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.

🏭 Production Scenario

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.

Follow-up Questions
What is __init_subclass__ and how does it compare to metaclasses? How does 'type' work as a metaclass? What causes a metaclass conflict??
ID: PY-ADV-001  ·  Difficulty: 8/10  ·  Level: Advanced
PY-ADV-002 How does Python’s memory management and garbage collection work?
Python Performance Advanced
8/10
Answer

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.

Deep Explanation

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.

Real-World Example

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.

⚠ Common Mistakes

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__.

🏭 Production Scenario

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.

Follow-up Questions
What is the difference between gc.collect() and del? How does __slots__ affect memory? What is weakref and when should you use it??
ID: PY-ADV-002  ·  Difficulty: 8/10  ·  Level: Advanced

PAGE 2 OF 2  ·  23 QUESTIONS TOTAL