Computational Complexity refers to the study of the resources (primarily time and space) required by an algorithm to solve a given computational problem as a function of the input size.
Python’s expressive syntax and rich standard library make it ideal for coding interviews.
enumerate()
Definition: enumerate()
returns pairs of (index, value) from an iterable, allowing you to loop with both the index and the value.
zip()
Definition: zip()
aggregates elements from two or more iterables (like lists or tuples), returning tuples with one element from each iterable.
map()
and filter()
Definition: map()
applies a function to every item in an iterable and returns a map object (which can be converted to a list). filter()
filters items in an iterable based on a function that returns True or False.
any()
and all()
Definition: any()
returns True if at least one element in the iterable is true. all()
returns True if all elements in the iterable are true.
sorted()
, reversed()
, and sum()/min()/max()
Definition: sorted()
returns a new sorted list from the items in an iterable. reversed()
returns an iterator that accesses the given sequence in the reverse order. sum()
, min()
, and max()
return the sum, minimum, and maximum of an iterable, respectively.
range()
Definition: range()
generates a sequence of numbers, commonly used for looping a specific number of times in for-loops.
range(n)
→ 0..n-1
itertools.chain()
Definition: itertools.chain()
takes multiple iterables and returns a single iterator that produces items from the first iterable until it is exhausted, then continues to the next iterable, and so on.
collections.Counter
Definition: collections.Counter
is a dict subclass for counting hashable objects. It returns a dictionary where elements are stored as keys and their counts as values.
collections.defaultdict
Definition: collections.defaultdict
is a dict subclass that provides a default value for nonexistent keys, avoiding KeyError.
collections.deque
Definition: collections.deque
is a double-ended queue that supports fast appends and pops from both ends.
heapq
(min-heap)Definition: The heapq
module provides an implementation of the min-heap queue algorithm, also known as the priority queue algorithm.
bisect
Definition: The bisect
module provides support for maintaining a list in sorted order without having to sort the list after each insertion.
key=
enumerate()
+ zip()
unpackingfunctools.lru_cache
for memoizationDefinition: functools.lru_cache
is a decorator that caches the results of function calls, so that when the same inputs occur again, the cached result is returned instead of recomputing.
range(n)
is 0..n-1
range(len(arr))
is correct<=
vs <
is
vs ==
[x for x in ...]
)Counter
, min
, max
, sum
, sorted
defaultdict
for groupingheapq.nlargest
/ nsmallest
for “top K” problemskey=
function@lru_cache
map
, filter
, zip
, enumerate