Python and algorithms interview questions
Quant developer and researcher interviews include quick-fire questions on Python behavior and algorithm costs. These are the gotchas that catch people: floor division, aliasing, default arguments, complexity and more.
1Python: print(0.1 + 0.2 == 0.3)
- True
- False
- Error
- None
Show the answer and solution
Answer: False
0.1 + 0.2 is 0.30000000000000004 in binary floating point.
2Python: a = [1, 2, 3]; b = a; b.append(4); print(len(a))
- 3
- 4
- Error
- 7
Show the answer and solution
Answer: 4
b and a name the same list object.
3Python: print([i * i for i in range(4)])
- [1, 4, 9, 16]
- [0, 1, 4, 9]
- [0, 1, 2, 3]
- Error
Show the answer and solution
Answer: [0, 1, 4, 9]
range(4) is 0, 1, 2, 3.
4Average cost of looking up a key in a Python dict?
- O(1)
- O(log n)
- O(n)
- O(n²)
Show the answer and solution
Answer: O(1)
Hash lookups are O(1) on average.
5Binary search on a sorted array of n items costs…
- O(1)
- O(log n)
- O(n)
- O(n log n)
Show the answer and solution
Answer: O(log n)
The range halves every comparison.
6Python: print(7 // 2)
- 3.5
- 3
- 4
- Error
Show the answer and solution
Answer: 3
7 // 2 floors 3.5 down to 3.
7Python: print('abc'[::-1])
- abc
- cba
- a
- Error
Show the answer and solution
Answer: cba
A step of −1 reverses the string.
8Python 3: print(type(3 / 2).__name__)
- int
- float
- Fraction
- Error
Show the answer and solution
Answer: float
3 / 2 is 1.5, a float.
9Python: print(bool([]))
- True
- False
- None
- Error
Show the answer and solution
Answer: False
An empty list evaluates to False.
10Python: print(sorted([3, 1, 2], reverse=True))
- [1, 2, 3]
- [3, 2, 1]
- [3, 1, 2]
- None
Show the answer and solution
Answer: [3, 2, 1]
Descending order: [3, 2, 1].
11Python: print('a' * 3)
- a3
- aaa
- Error
- ['a', 'a', 'a']
Show the answer and solution
Answer: aaa
'a' * 3 is 'aaa'.
12Python: print(10 % 3)
- 3
- 1
- 0
- 3.33
Show the answer and solution
Answer: 1
10 = 3 × 3 + 1.
13Python: print(len('hello world'.split()))
- 1
- 2
- 11
- 10
Show the answer and solution
Answer: 2
['hello', 'world'] has 2 items.
14Finding two numbers in an array that sum to a target, using a hash set of values seen so far, takes…
- O(1)
- O(log n)
- O(n)
- O(n²)
Show the answer and solution
Answer: O(n)
For each number, check whether target − x was seen: O(n) time on average.
15Python: what does print(len(range(0, 10, 1))) show?
Show the answer and solution
Answer: 10
The sequence starts at 0, steps by 1 and stops before 10: 10 values.
16Python: what does print(1 << 1) show?
Show the answer and solution
Answer: 2
<< shifts the bits left; each shift doubles the number. Starting from 1 and shifting 1 times gives 2^1 = 2.
17Python: what does print(len(range(0, 10, 2))) show?
Show the answer and solution
Answer: 5
The sequence starts at 0, steps by 2 and stops before 10: 5 values.
18Python: what does print(1 << 3) show?
Show the answer and solution
Answer: 8
<< shifts the bits left; each shift doubles the number. Starting from 1 and shifting 3 times gives 2^3 = 8.
19What does pushing onto a binary heap of n items cost?
- O(1)
- O(log n)
- O(n)
- O(n log n)
Show the answer and solution
Answer: O(log n)
At most the tree's height, O(log n).
20NumPy: np.std(x) uses which denominator by default?
- n
- n − 1
- n + 1
- √n
Show the answer and solution
Answer: n
ddof = 0 by default; pass ddof=1 for the sample SD.
21Python: print(-7 // 2)
- -3
- -4
- -3.5
- 3
Show the answer and solution
Answer: -4
−3.5 floors to −4.
22The best possible worst-case cost of a comparison sort is…
- O(n)
- O(n log n)
- O(log n)
- O(n²)
Show the answer and solution
Answer: O(n log n)
Distinguishing n! orderings needs about log₂(n!) ≈ n log n comparisons.
23Python: print(len(set([1, 1, 2, 3, 3, 3])))
- 6
- 3
- 2
- 1
Show the answer and solution
Answer: 3
The set is {1, 2, 3}.
24Python: x = (1); print(type(x).__name__)
- tuple
- int
- list
- Error
Show the answer and solution
Answer: int
You need a trailing comma: (1,) is a tuple.
25Python: what does print(-17 // 4) show?
Show the answer and solution
Answer: -5
// is floor division: it divides, then rounds down toward −∞ (not toward zero). -17 / 4 = -4.25, which rounds down to -5. Watch out: rounding toward zero would give -4.
26Python: what does print(-17 % 4) show?
Show the answer and solution
Answer: 3
Python defines a % b = a − b × (a // b), so the result takes the sign of b. Here -17 // 4 = -5, so -17 % 4 = -17 − (4) × (-5) = 3.
27Python: what does print(bin(20).count('1')) show?
Show the answer and solution
Answer: 2
20 = 0b10100, which has 2 ones.
28Python: what does print(-13 // 4) show?
Show the answer and solution
Answer: -4
// is floor division: it divides, then rounds down toward −∞ (not toward zero). -13 / 4 = -3.25, which rounds down to -4. Watch out: rounding toward zero would give -3.
29Python: what does print(-13 % 4) show?
Show the answer and solution
Answer: 3
Python defines a % b = a − b × (a // b), so the result takes the sign of b. Here -13 // 4 = -4, so -13 % 4 = -13 − (4) × (-4) = 3.
30Python: what does print(bin(40).count('1')) show?
Show the answer and solution
Answer: 2
40 = 0b101000, which has 2 ones.
31Python: what does print(-9 // 4) show?
Show the answer and solution
Answer: -3
// is floor division: it divides, then rounds down toward −∞ (not toward zero). -9 / 4 = -2.25, which rounds down to -3. Watch out: rounding toward zero would give -2.
32Python: what does print(-9 % 4) show?
Show the answer and solution
Answer: 3
Python defines a % b = a − b × (a // b), so the result takes the sign of b. Here -9 // 4 = -3, so -9 % 4 = -9 − (4) × (-3) = 3.