Interview prep
Python interview questions
63 of the most common Python questions for data and AI interviews — each with a worked answer, the trap to avoid, and a link to learn it properly. Core language, data structures, and idioms for data work.
Filter by role
- Write a function to check whether two strings are anagrams of each other. What is the optimal time complexity? Easy ·Google·Amazon·Microsoft
- What is the difference between *args and **kwargs, and when would you use each? Easy ·Google·Amazon·Meta
- What is a context manager and how does the with statement work? Easy ·Amazon·Microsoft·Databricks
- What is the most Pythonic way to count word frequencies in a string, and what does Counter return for missing keys? Easy ·Google·Amazon·Meta
- How do you define and raise custom exceptions in Python? Easy ·Shopify·Twilio·HubSpot
- What does `@dataclass` give you over a plain class, and what are its main configuration options? Easy ·Google·Meta·Amazon
- How do you use dict comprehensions effectively, and what are common patterns in data work? Easy ·Amazon·Google·Meta
- When and how do you use enumerate() and zip() in Python, and what are common mistakes when using them together? Easy
- Write a function that returns all duplicate values in a list. What is the optimal time complexity? Easy ·Amazon·Microsoft·Stripe
- What does it mean for functions to be first-class objects in Python? Easy ·Google·Meta·Amazon
- How do you flatten a nested list in Python, and how does the approach differ for one level deep versus arbitrarily deep nesting? Easy
- How do you group a list of items by a property using defaultdict, and when would you use it over a plain dict? Easy ·Airbnb·Lyft·Stripe
- What is the difference between an instance method, a class method, and a static method in Python? Easy ·Google·Amazon·Meta
- What is the difference between `is` and `==` in Python, and when does `is` give a surprising result? Easy ·Amazon·Google·Microsoft
- When should you use lambda, map, filter, and reduce — and when should you avoid them? Easy ·Amazon·Google
- How do list, dict, and set comprehensions work in Python, and when should you avoid them? Easy ·Amazon·Google·Meta
- Are list comprehensions faster than equivalent for-loops in Python, and when should you prefer a generator expression instead? Easy
- Given a new data problem, how do you decide whether to use a list, dict, or set? Easy ·Amazon·Google·Meta
- How does Python list slicing work, including step and negative indices? Easy ·Amazon·Google
- When would you use a Python list versus a NumPy array, and what are the performance trade-offs? Easy ·Google·Meta·Amazon
- What is the difference between a list and a tuple, and when should you use each? Easy ·Amazon·Google·Microsoft
- Implement Fibonacci with memoization in Python. What problem does memoization solve, and what is the time complexity before and after? Easy ·Google·Amazon·Meta
- Which Python built-in types are mutable and which are immutable, and why does it matter? Easy ·Amazon·Microsoft·Google
- What is the difference between class attributes and instance attributes in Python, and why are mutable class attributes dangerous? Easy ·Google·Amazon·Meta
- What is the mutable default argument trap in Python, and how do you fix it? Easy ·Google·Amazon·Meta
- What is the difference between positional and keyword arguments in Python? Easy ·Amazon·Microsoft
- How does the `@property` decorator work in Python, and when should you prefer it over a plain attribute? Easy ·Google·Amazon·Microsoft
- What are .pyc files and what role does Python bytecode play? Easy
- In Python, do variables store values or references to objects? Easy ·Amazon·Microsoft·IBM
- Write Python to read a CSV file line by line and compute a column aggregate without loading the entire file into memory. Easy ·Amazon·Databricks
- How do you reverse a list and remove duplicates in Python, and what are the performance implications of each approach? Easy
- What set operations does Python support, and where are they practically useful in data work? Easy
- How do you sort a list of dictionaries by a specific key in Python, and what is the difference between sorted() and list.sort()? Easy ·Amazon·Airbnb
- What is the difference between `__str__` and `__repr__` in Python, and which should you implement first? Easy ·Google·Amazon·Meta
- Solve the Two Sum problem. Given a list of integers and a target, return the indices of the two numbers that add up to the target. What is the optimal complexity? Easy ·Google·Amazon·Meta
- Why do Python projects use virtual environments, and what are the modern tools for dependency management? Easy ·Google·Shopify·Databricks
- Why is NumPy significantly faster than Python for-loops for numerical computation, and what is vectorization? Easy ·Google·Meta·Netflix
- Why are Python strings immutable, and how does string interning affect memory? Easy
- What are Abstract Base Classes in Python, how do you define them, and how do they relate to duck typing? Medium ·Google·Stripe·Palantir
- What is a closure in Python, and what problem does it solve? Medium ·Google·Meta·Stripe
- When should you use composition instead of inheritance in Python, and what are the design signals for each? Medium ·Google·Meta·Stripe
- What is the difference between CPU-bound and I/O-bound work, and how does the choice affect concurrency strategy in Python? Medium ·Netflix·Uber·Palantir
- How does Python's dict work internally, and what makes a good hash key? Medium ·Google·Amazon·Meta
- What is the contract between `__eq__` and `__hash__` in Python, and what breaks when you define only one? Medium ·Google·Meta·Amazon
- What do the else and finally clauses of a try block do, and when does finally NOT run? Medium ·Google·Stripe·Atlassian
- Why is functools.wraps necessary when writing decorators? Medium ·Stripe·Airbnb·Uber
- What is the difference between a generator and a list, and when should you prefer a generator? Medium ·Google·Amazon·Meta
- What do the global and nonlocal keywords do, and when should you use them? Medium
- What is the difference between `__new__` and `__init__` in Python, and when would you override `__new__`? Medium ·Google·Stripe·Palantir
- What is the difference between an iterable and an iterator in Python? Medium ·Amazon·Microsoft·Meta
- Explain Python's LEGB scope rule with an example. Medium ·Amazon·Google·Microsoft
- How does Python's Method Resolution Order (MRO) work, and what is the C3 linearization algorithm? Medium ·Google·Meta·Stripe
- How does Python's mutable vs immutable distinction affect function arguments and default values? Medium ·Amazon·Google·Microsoft
- How does Python manage memory — reference counting and the garbage collector? Medium ·Google·Meta·Palantir
- What is the difference between a shallow copy and a deep copy, and when does it matter? Medium ·Amazon·Google·Microsoft
- What does `__slots__` do in Python, and when should you use it? Medium ·Google·Meta·Stripe
- How does `super()` work in Python, and why is the zero-argument form preferred over `super(ClassName, self)`? Medium ·Google·Meta·Stripe
- When should you use threading versus multiprocessing in Python? Medium ·Uber·Netflix·Airbnb
- What is a decorator in Python, and how does it work under the hood? Medium ·Google·Amazon·Meta
- What is Python's GIL and why does it exist? Medium ·Google·Meta·Amazon
- How does yield differ from return, and what happens to a function's state when it yields? Medium ·Google·Amazon·Databricks
- How does asyncio differ from threading, and when would you choose one over the other? Hard ·Stripe·Cloudflare·Discord
- How do you write a decorator that accepts its own arguments? Hard ·Google·Stripe·Airbnb
No questions tagged for that role yet.