Python Interview Questions
This document provides a curated list of Python interview questions commonly asked in technical interviews for Data Scientists, Backend Engineers, and Python Developers. It covers core concepts, internals, concurrency, and advanced language features.
This is updated frequently but right now this is the most exhaustive list of type of questions being asked.
| Sno | Question Title | Practice Links | Companies Asking | Difficulty | Topics |
|---|---|---|---|---|---|
| 1 | What is Python? Interpreted or Compiled? | Python Docs | Google, Amazon, Meta | Easy | Basics |
| 2 | What is PEP 8? | PEP 8 | Most Tech Companies | Easy | Standards |
| 3 | Mutable vs Immutable types in Python | Real Python | Google, Amazon, Microsoft | Easy | Data Structures |
| 4 | Explain List vs Tuple | GeeksforGeeks | Most Tech Companies | Easy | Data Structures |
| 5 | What is a Dictionary in Python? | Python Docs | Most Tech Companies | Easy | Data Structures |
| 6 | How is memory managed in Python? | Real Python | Google, Amazon, Meta, Netflix | Medium | Internals, Memory |
| 7 | What is the Global Interpreter Lock (GIL)? | Real Python | Google, Amazon, Meta, Apple | Hard | Internals, Concurrency |
| 8 | Explain Garbage Collection in Python | Python Docs | Google, Amazon, Spotify | Medium | Internals, GC |
| 9 | What are decorators? | Real Python | Google, Amazon, Meta, Netflix | Medium | Functions, Advanced |
| 10 | Difference between @staticmethod and @classmethod | Stack Overflow | Google, Amazon, Meta | Easy | OOP |
| 11 | What are lambda functions? | Real Python | Most Tech Companies | Easy | Functions |
| 12 | Explain *args and **kwargs | Real Python | Most Tech Companies | Easy | Functions |
| 13 | What are generators and the yield keyword? | Real Python | Google, Amazon, Meta, Netflix | Medium | Iterators, Generators |
| 14 | Difference between range() and xrange() | GeeksforGeeks | Legacy Companies | Easy | Python 2 vs 3 |
| 15 | What is a docstring? | Python Docs | Most Tech Companies | Easy | Documentation |
| 16 | How to copy an object? (Deep vs Shallow copy) | Real Python | Google, Amazon, Meta | Medium | Objects, Memory |
| 17 | What is __init__? | Python Docs | Most Tech Companies | Easy | OOP |
| 18 | What is __str__ vs __repr__? | Stack Overflow | Google, Amazon, Meta | Medium | OOP, Magic Methods |
| 19 | How typically does inheritance work in Python? | Real Python | Most Tech Companies | Easy | OOP |
| 20 | What is Multiple Inheritance and MRO? | Python Docs | Google, Amazon, Meta | Hard | OOP, MRO |
| 21 | Explain is vs == | Real Python | Most Tech Companies | Easy | Operators |
| 22 | What are packing and unpacking? | Real Python | Google, Amazon, Meta | Medium | Basics |
| 23 | How to handle exceptions? (try/except/finally) | Python Docs | Most Tech Companies | Easy | Error Handling |
| 24 | What are assertions? | Real Python | Google, Amazon, Meta | Medium | Debugging |
| 25 | What implies pass statement? | Python Docs | Most Tech Companies | Easy | Control Flow |
| 26 | What are Context Managers (with statement)? | Real Python | Google, Amazon, Meta, Netflix | Medium | Context Managers |
| 27 | Difference between lists and arrays (array module)? | GeeksforGeeks | Google, Amazon | Easy | Data Structures |
| 28 | What is a Set? | Python Docs | Most Tech Companies | Easy | Data Structures |
| 29 | How does Python handle large numbers? | Python Docs | Google, Amazon, HFT Firms | Medium | Internals |
| 30 | What are namespaces? | Real Python | Google, Amazon, Meta | Medium | Scoping |
| 31 | Explain Local, Global, and Nonlocal scope | Real Python | Google, Amazon, Meta | Medium | Scoping |
| 32 | What is a module vs a package? | Real Python | Most Tech Companies | Easy | Modules |
| 33 | How to use pip? | Python Docs | Most Tech Companies | Easy | Packaging |
| 34 | What is venv/virtualenv? | Real Python | Most Tech Companies | Easy | Environment |
| 35 | How to read/write files? | Python Docs | Most Tech Companies | Easy | I/O |
| 36 | What is pickling and unpickling? | Python Docs | Google, Amazon, Meta | Medium | Serialization |
| 37 | What are iterators and lazy evaluation? | Real Python | Google, Amazon, Meta | Medium | Iterators |
| 38 | What is the zip() function? | Real Python | Most Tech Companies | Easy | Built-ins |
| 39 | What is map() and filter()? | Real Python | Google, Amazon, Meta | Easy | Functional Programming |
| 40 | What is functools.reduce()? | Real Python | Google, Amazon, Meta | Medium | Functional Programming |
| 41 | Difference between .py and .pyc files | Stack Overflow | Google, Amazon, Meta | Medium | Internals |
| 42 | What is __name__ == "__main__"? | Real Python | Most Tech Companies | Easy | Modules |
| 43 | How to create a singleton class? | GeeksforGeeks | Google, Amazon, Meta | Hard | Patterns |
| 44 | What are Metaclasses? | Real Python | Google, Amazon, Meta, Netflix | Hard | Metaclasses |
| 45 | What is __slots__? | GeeksforGeeks | Google, Amazon, HFT Firms | Hard | Optimization, Memory |
| 46 | Difference between func and func() | Stack Overflow | Google, Amazon | Easy | Functions |
| 47 | What is slicing? | Python Docs | Most Tech Companies | Easy | Data Structures |
| 48 | Negative indexing in Python | GeeksforGeeks | Most Tech Companies | Easy | Indexing |
| 49 | What is a hash map in Python? | Python Docs | Most Tech Companies | Easy | Data Structures |
| 50 | Does Python support pointer arithmetic? | Stack Overflow | Google, Amazon | Medium | Internals |
| 51 | What are default arguments? Pitfalls? | Real Python | Google, Amazon, Meta | Medium | Functions, Pitfalls |
| 52 | What is collections module? | Python Docs | Google, Amazon, Meta | Medium | Standard Library |
| 53 | Explain defaultdict and Counter | Real Python | Google, Amazon, Meta | Medium | Standard Library |
| 54 | What is NamedTuple? | Real Python | Google, Amazon, Meta | Medium | Data Structures |
| 55 | What is itertools? | Real Python | Google, Amazon, Meta, HFT Firms | Hard | Iterators |
| 56 | What are Threading vs Multiprocessing? | Real Python | Google, Amazon, Meta, Netflix | Medium | Concurrency |
| 57 | What is Asyncio? | Real Python | Google, Amazon, Meta, Netflix | Hard | Concurrency |
| 58 | Difference between await and yield | Stack Overflow | Google, Amazon, Meta | Hard | Concurrency |
| 59 | What is a coroutine? | Python Docs | Google, Amazon, Meta | Hard | Concurrency |
| 60 | How to debug python code? (pdb) | Real Python | Most Tech Companies | Medium | Debugging |
| 61 | What are type hints (Type Annotation)? | Real Python | Google, Amazon, Meta, Microsoft | Medium | Typing |
| 62 | What is MyPy? | MyPy Docs | Google, Amazon, Meta | Medium | Typing |
| 63 | What is a Data Class? | Real Python | Google, Amazon, Meta | Medium | Data Structures |
| 64 | Difference between copy() and deepcopy() | Python Docs | Google, Amazon, Meta | Medium | Memory |
| 65 | How to reverse a list? | Real Python | Most Tech Companies | Easy | Data Structures |
| 66 | String formatting options (f-strings vs format) | Real Python | Most Tech Companies | Easy | Strings |
| 67 | What is sys.path? | Python Docs | Google, Amazon, Meta | Medium | Modules |
| 68 | What is __call__ method? | GeeksforGeeks | Google, Amazon, Meta | Medium | OOP |
| 69 | What is __new__ vs __init__? | Stack Overflow | Google, Amazon, Meta | Hard | OOP |
| 70 | What is Monkey Patching? | Stack Overflow | Google, Amazon, Meta | Medium | Dynamic Programming |
| 71 | What is Duck Typing? | Real Python | Google, Amazon, Meta | Medium | OOP |
| 72 | What is dir() function? | Python Docs | Most Tech Companies | Easy | Introspection |
| 73 | What is help() function? | Python Docs | Most Tech Companies | Easy | Introspection |
| 74 | What is enumerate()? | Real Python | Most Tech Companies | Easy | Iteration |
| 75 | How to merge two dicts? | Real Python | Most Tech Companies | Easy | Data Structures |
| 76 | Comprehensions (List, Dict, Set) | Real Python | Most Tech Companies | Medium | Syntax |
| 77 | What is __future__ module? | Python Docs | Google, Amazon | Medium | Compatibility |
| 78 | What is pd.DataFrame vs Python List? | Pandas Docs | Most Tech Companies | Easy | Data Analysis |
| 79 | How to handle circular imports? | Stack Overflow | Google, Amazon, Meta | Medium | Modules |
| 80 | What is getattr, setattr, hasattr? | Python Docs | Google, Amazon, Meta | Medium | Introspection |
| 81 | What is __dict__ attribute? | Python Docs | Google, Amazon, Meta | Medium | Internals |
| 82 | Explain the with statement protocol (__enter__, __exit__) | Real Python | Google, Amazon, Meta | Hard | Context Managers |
| 83 | What are property decorators? | Real Python | Google, Amazon, Meta | Medium | OOP |
| 84 | What is Operator Overloading? | GeeksforGeeks | Google, Amazon, Meta | Medium | OOP |
| 85 | What is ternary operator in Python? | Real Python | Most Tech Companies | Easy | Syntax |
| 86 | How to optimize Python code speed? | Wiki | Google, Amazon, HFT Firms | Hard | Performance |
| 87 | Why is Python slow? | Real Python | Google, Amazon, Meta | Medium | Performance |
| 88 | What is PyPy? | PyPy | Google, Amazon | Hard | Interpreters |
| 89 | What is Cython? | Cython | Google, Amazon, HFT Firms | Hard | Performance |
| 90 | Difference between os and sys modules? | Stack Overflow | Google, Amazon | Medium | Standard Library |
| 91 | What is re module (Regular Expressions)? | Real Python | Most Tech Companies | Medium | Standard Library |
| 92 | [HARD] How does Reference Counting vs Garbage Collection work? | DevGuide | Google, Meta, Netflix | Hard | Internals |
| 93 | [HARD] How to implement custom Metaclass? | Real Python | Google, Meta, Frameworks | Hard | Metaprogramming |
| 94 | [HARD] Explain method resolution order (MRO) C3 algorithm | Python Docs | Google, Meta | Hard | OOP Internals |
| 95 | [HARD] How to avoid the GIL (multiprocessing, C extensions)? | Real Python | Google, Amazon, HFT Firms | Hard | Performance |
| 96 | [HARD] Memory leaks in Python: Causes and fixes | TechBlog | Google, Meta, Netflix | Hard | Memory |
| 97 | [HARD] How asyncio event loop works internally | Real Python | Google, Meta, Netflix | Hard | Asyncio Internals |
| 98 | [HARD] Difference between Threading and Asyncio concurrency models | Real Python | Google, Meta, Netflix | Hard | Concurrency |
| 99 | [HARD] How to implement non-blocking I/O? | Python Docs | Google, Amazon | Hard | I/O |
| 100 | [HARD] What is __import__ vs importlib? | Python Docs | Google, Meta, Frameworks | Hard | Internals |
| 101 | [HARD] How are Python dictionaries implemented (Hash Table)? | PyCon Talk | Google, Meta, Amazon | Hard | Internals |
| 102 | [HARD] Explain descriptor protocol | Real Python | Google, Meta, Frameworks | Hard | Descriptors |
| 103 | [HARD] How to use sys.settrace for debugging/profiling? | Python Docs | Google, Meta | Hard | Internals |
| 104 | [HARD] Difference between Process and Thread in Python context | Real Python | Google, Amazon, Meta | Hard | OS Concepts |
| 105 | [HARD] How to manage weak references (weakref)? | Python Docs | Google, Meta | Hard | Memory |
| 106 | [HARD] What is the Disassembler (dis module)? | Python Docs | Google, Meta | Hard | Bytecode |
| 107 | [HARD] How to optimize dictionary memory usage? | Stack Overflow | Google, Amazon | Hard | Memory |
| 108 | [HARD] Explain Coroutines vs Generators | Real Python | Google, Meta | Hard | Concurrency |
| 109 | [HARD] How to implement custom context managers (contextlib)? | Python Docs | Google, Amazon | Hard | Advanced Patterns |
| 110 | [HARD] How to perform zero-copy data transfer (Buffer Protocol)? | Python Docs | Google, HFT Firms | Hard | Performance |
Code Examples
1. Decorator for Timing Functions
import time
import functools
def timer_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
run_time = end_time - start_time
print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
return result
return wrapper
@timer_decorator
def complex_calculation(n):
return sum(i**2 for i in range(n))
complex_calculation(1000000)
2. Context Manager for Files
class FileManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
self.file = None
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exc_type, exc_value, exc_traceback):
if self.file:
self.file.close()
# Usage
# with FileManager('test.txt', 'w') as f:
# f.write('Hello, World!')
3. Asynchronous Pattern
import asyncio
async def fetch_data(delay, id):
print(f"Fetching data {id}...")
await asyncio.sleep(delay) # Simulate I/O op
print(f"Data {id} fetched")
return {"id": id, "data": "sample"}
async def main():
# Run tasks concurrently
tasks = [fetch_data(1, 1), fetch_data(2, 2), fetch_data(1.5, 3)]
results = await asyncio.gather(*tasks)
print(results)
# asyncio.run(main())
Questions asked in Google interview
- Explain the Global Interpreter Lock (GIL) and its impact on multi-threading.
- How does Python's garbage collection mechanism work? (Reference counting vs Generational GC).
- Write a custom decorator that caches function results (Memoization).
- How would you debug a memory leak in a long-running Python process?
- Explain the method resolution order (MRO) works with multiple inheritance.
- Write code to implement a thread-safe singleton.
- How to optimize a CPU-bound Python script?
- Explain the key differences between Python 2 and Python 3.
- How to implement a context manager using
contextlib. - Write code to parse a large log file without loading it entirely into memory.
Questions asked in Meta interview
- How are dictionaries implemented in Python? (Hash collision handling).
- Explain the difference between
__new__and__init__. - Write code to flatten a deeply nested dictionary.
- How does
asynciodiffer from threading? When to use which? - Explain the concept of metaclasses and a use case.
- Write a generator that yields Fibonacci numbers indefinitely.
- How to handle circular imports in a large project?
- Explain the descriptor protocol and how properties work.
- How would you implement a custom iterator?
- Write code to validate and parse JSON data using
dataclassesorpydantic.
Questions asked in Amazon interview
- Write code to reverse a string without using built-in methods.
- Explain the difference between deep copy and shallow copy.
- How to handle exceptions in a large-scale application?
- Write code to find the most frequent element in a list efficiently.
- Explain the use of
*argsand**kwargs. - How to implement a producer-consumer problem using
queue? - Explain the difference between
@staticmethod,@classmethod, and instance methods. - Write code to sort a list of dictionaries by a specific key.
- How does variable scope work in Python (LEGB rule)?
- Explain what
if __name__ == "__main__":does.
Questions asked in Netflix interview
- How to optimize Python for high-throughput network applications?
- Explain the internals of CPython execution loop.
- Write code to implement a rate limiter using Redis and Python.
- How to handle dependency management in a microservices architecture?
- Explain how
gunicornoruwsgiworks with Python web apps. - Write code to implement async HTTP requests using
aiohttp. - How to profile Python code to find bottlenecks? (cProfile, py-spy).
- Explain the challenge of serialization (pickling) in distributed systems.
- How to implement rigorous unit testing with
pytest? - Write code to process a stream of data using generators.