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 5 questions · Advanced · Python

Clear all filters
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-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