Designations: J — junior, M — middle, S — senior. The code is in Python, but interviewers usually allow any language you know.
In this part:
Algorithms and data structures
J. What is the complexity of O(n), O(log n), O(n²)?
How time (or memory) grows as input data grows. O(1) — does not depend on size (access to an array element by index, dictionary on average). O(log n) — binary search on a sorted array. O(n) — one pass through the list. O(n log n) — good sorts. O(n²) — nested loop over the same data. Key practical conclusion: at a million elements O(n²) — this is already a problem.
J. How is a dictionary (hash table) structured and why is searching in it fast?
The key is converted by the hash function into a number by which the array cell is computed. Lookup, insertion, and deletion are on average O(1). Collisions (different keys in one cell) are resolved by chains or by open addressing. Hence the requirement: the key must be immutable and hashable (in Python — string, number, tuple, but not a list).
Problem J: two numbers with a given sum
Find the indices of two elements whose sum equals target.
def two_sum(nums: list[int], target: int) -> tuple[int, int] | None:
seen: dict[int, int] = {} # значение → индекс
for i, x in enumerate(nums):
if target - x in seen:
return seen[target - x], i
seen[x] = i
return None
assert two_sum([2, 7, 11, 15], 9) == (0, 1)
assert two_sum([3, 3], 6) == (0, 1)
assert two_sum([1, 2], 10) is None