Interview Q and A
Preparing for Python interviews requires both conceptual understanding and the ability to explain ideas clearly. The following Q&A covers the topics most f...
Python Interview Questions with Concise Answers
Preparing for Python interviews requires both conceptual understanding and the ability to explain ideas clearly. The following Q&A covers the topics most frequently asked in screening rounds and phone interviews.
Q: What is the difference between a list and a tuple? A: Lists are mutable (you can add, remove, or change elements). Tuples are immutable (once created, their elements cannot change). Tuples are faster, use less memory, and can be used as dictionary keys. Use tuples for fixed collections of related values (coordinates, RGB, records).
Q: What are *args and **kwargs? A: *args captures extra positional arguments as a tuple. **kwargs captures extra keyword arguments as a dictionary. Together they make functions accept a variable number of arguments: def func(*args, **kwargs): print(args) # tuple of positional args print(kwargs) # dict of keyword args
Q: What is a lambda function? A: An anonymous one-line function: square = lambda x: x**2. Useful for short callbacks in sorted(), map(), filter(), but avoid them for complex logic.
Q: How does Python manage memory? A: Through reference counting and a cyclic garbage collector. Every object has a reference count. When it drops to zero the object is deallocated. The gc module handles reference cycles.
Q: What is the GIL? A: The Global Interpreter Lock prevents multiple threads from executing Python bytecode simultaneously in CPython. This limits CPU-parallelism with threads. Use multiprocessing or async for compute-bound parallelism; threads are fine for I/O-bound tasks.
Q: Explain list comprehension versus map()/filter(). A: List comprehension is generally more readable: [x*2 for x in range(10) if x%2==0]. map() and filter() return iterators and avoid creating an intermediate list, which saves memory for large sequences.
Q: What is monkey patching? A: Dynamically replacing or adding attributes or methods to an existing class or module at runtime. Useful for testing (replacing a method with a mock) but dangerous in production code.
Practical tip: Structure your interview answers using the STAR pattern for behavioral questions and the think-aloud technique for coding questions. Verbalizing your thought process is often weighted as heavily as the final solution.
Exam tip: Know Python's built-in functions by heart: len, range, enumerate, zip, map, filter, sorted, reversed, any, all, min, max, sum, isinstance, type, id, dir, help. These appear in virtually every coding assessment.