Interview frequency: …
Short answer
Measure first, then reduce unnecessary temporary objects, use built-in structures and functions written in C, or move the computation to NumPy or a compiled extension if the profiler shows the loop is the bottleneck.
Explanation
Every object in Python has a memory and time overhead. Common techniques: hoisting invariant operations out of the loop, using built-in functions (sum, any, sorted) instead of manual loops, using __slots__ or tuples for many small objects, and using array or NumPy for many numbers instead of a list of int objects. Since Python 3.11 the interpreter itself has become faster thanks to adaptive specialization. But always start by improving the algorithm and I/O; fine-grained tweaks hurt readability and may have no effect.
Code example
import numpy as np
# Slow: millions of objects of type float
prices = [p * 1.14 for p in raw_prices]
# Faster: one operation on a contiguous array in C
prices = np.asarray(raw_prices) * 1.14
# Move the constant outside the loop
rate = config.tax_rate
total = sum(item.price * rate for item in items)Common mistakes
- Applying micro-optimizations that hurt readability before confirming they are the bottleneck.
- Ignoring that the problem is an O(n²) algorithm, not object overhead.