Python's built-in sort is stable and uses Timsort.
Question 15: What is Timsort?
Ans
Timsort is the hybrid stable sorting algorithm used by Python's built-in list sorting and sorted(). It performs well on partially ordered real-world data.
Example
python
values = [5, 1, 4, 2, 3]
values.sort()
Important Point
The implementation details are optimized internally; interview answers should focus on stability and typical O(n log n) worst-case behavior.
Question 16: What is bisect?
Ans
The bisect module provides binary-search-based insertion and search helpers for maintaining sorted lists.
Searching is O(log n), but inserting into a Python list is O(n) because elements may need to shift.
Question 17: What is heapq?
Ans
heapq provides a min-heap implementation over a Python list and is useful for priority queues and top-k problems.
Example
python
import heapq
h = [5, 1, 3]
heapq.heapify(h)
print(heapq.heappop(h))
Important Point
The heap list is not fully sorted; only the heap property is guaranteed.
Question 18: What is itertools?
Ans
itertools contains efficient iterator-building tools such as chain, combinations, permutations, groupby, and product.
Example
python
from itertools import combinations
print(list(combinations([1,2,3], 2)))
Important Point
Many itertools functions are lazy, which can save memory for large pipelines.
Question 19: What is functools?
Ans
functools provides higher-order function utilities such as reduce, partial, wraps, cache, and singledispatch.
Example
python
from functools import partial
double = partial(pow, exp=2)
print(double(3))
Important Point
Understand the resulting callable signature/behavior when using partial or decorators.
Question 20: What is lru_cache?
Ans
functools.lru_cache memoizes function results for hashable arguments, avoiding repeated computation.
Example
python
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2: return n
return fib(n-1) + fib(n-2)
print(fib(30))
Important Point
Caching trades memory for speed and only works when the function's result is appropriate to cache.
Question 21: What is caching?
Ans
Caching stores previously computed or fetched results so future requests can be served faster or with less work.
Example
python
cache = {}
def square(n):
if n not in cache:
cache[n] = n * n
return cache[n]
Important Point
A cache needs an invalidation or eviction strategy when data can become stale or memory is limited.
Question 22: Why is list indexing O(1)?
Ans
Python lists are dynamic arrays in CPython, so an index can be translated to an offset in the underlying array for direct access.
Example
python
values = [10, 20, 30]
print(values[2])
Important Point
This describes the common CPython list implementation; inserting at the front is still O(n).
Question 23: Why is dictionary lookup usually O(1)?
Ans
Python dictionaries use hash-table techniques to locate a key by its hash, giving average-case constant-time lookup.
Example
python
data = {"id": 101}
print(data["id"])
Important Point
Worst-case behavior can differ, and hashability/equality rules determine whether a key can be used.
Question 24: Why must dictionary keys be hashable?
Ans
A dictionary uses a key's hash to choose a lookup location and equality to resolve matching keys. A mutable object whose hash changes would break those assumptions.
Example
python
key = (1, 2)
data = {key: "point"}
print(data[key])
Important Point
Hashability requires a stable hash consistent with equality during the key's lifetime.
Question 25: What happens when a key is assigned twice?
Ans
Assigning an existing dictionary key replaces its associated value while keeping that key's identity in the mapping.
Example
python
d = {"role": "user"}
d["role"] = "admin"
print(d)
Important Point
A dictionary cannot contain two separate entries with equal keys.
Question 26: What is insertion order in dictionaries?
Ans
Python dictionaries preserve insertion order as a language guarantee in modern Python versions. Updating an existing key does not move it to a new position by default.
Example
python
d = {"a": 1, "b": 2}
d["a"] = 9
print(list(d))
Important Point
Do not confuse insertion-order preservation with sorting.
Question 27: Why are sets unordered?
Ans
Sets are designed around hashing and uniqueness rather than positional access. Their iteration order should not be treated as a meaningful sorted or insertion order.
Example
python
s = {3, 1, 2}
print(2 in s)
Important Point
Do not rely on a particular printed set order.
Question 28: What is memoization?
Ans
Memoization caches function results keyed by inputs so repeated calls can avoid repeated computation.
Example
python
from functools import cache
@cache
def square(n): return n*n
print(square(10))
Important Point
Memoization is appropriate for deterministic computations with reusable results and manageable cache size.
Question 29: What is functools.partial?
Ans
partial() creates a new callable with some arguments of another callable pre-filled.
Example
python
from functools import partial
def power(base, exp): return base ** exp
square = partial(power, exp=2)
print(square(5))
Important Point
Partial application can simplify callbacks and configuration-heavy APIs.