Edge Sign in

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.

32 sample questions with solutions. Edge has 233 python and algorithms problems to practice, with a daily puzzle, timed games and a mock assessment. Practice them free.

1Python: print(0.1 + 0.2 == 0.3)

  1. True
  2. False
  3. Error
  4. None

Warm-up

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))

  1. 3
  2. 4
  3. Error
  4. 7

Warm-up

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. [1, 4, 9, 16]
  2. [0, 1, 4, 9]
  3. [0, 1, 2, 3]
  4. Error

Warm-up

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?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n²)

Warm-up

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…

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n log n)

Warm-up

Show the answer and solution

Answer: O(log n)

The range halves every comparison.

6Python: print(7 // 2)

  1. 3.5
  2. 3
  3. 4
  4. Error

Warm-up

Show the answer and solution

Answer: 3

7 // 2 floors 3.5 down to 3.

7Python: print('abc'[::-1])

  1. abc
  2. cba
  3. a
  4. Error

Warm-up

Show the answer and solution

Answer: cba

A step of −1 reverses the string.

8Python 3: print(type(3 / 2).__name__)

  1. int
  2. float
  3. Fraction
  4. Error

Warm-up

Show the answer and solution

Answer: float

3 / 2 is 1.5, a float.

9Python: print(bool([]))

  1. True
  2. False
  3. None
  4. Error

Warm-up

Show the answer and solution

Answer: False

An empty list evaluates to False.

10Python: print(sorted([3, 1, 2], reverse=True))

  1. [1, 2, 3]
  2. [3, 2, 1]
  3. [3, 1, 2]
  4. None

Warm-up

Show the answer and solution

Answer: [3, 2, 1]

Descending order: [3, 2, 1].

11Python: print('a' * 3)

  1. a3
  2. aaa
  3. Error
  4. ['a', 'a', 'a']

Warm-up

Show the answer and solution

Answer: aaa

'a' * 3 is 'aaa'.

12Python: print(10 % 3)

  1. 3
  2. 1
  3. 0
  4. 3.33

Warm-up

Show the answer and solution

Answer: 1

10 = 3 × 3 + 1.

13Python: print(len('hello world'.split()))

  1. 1
  2. 2
  3. 11
  4. 10

Warm-up

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…

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n²)

Warm-up

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?

Python rangesWarm-up

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?

BitsWarm-up

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?

Python rangesWarm-up

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?

BitsWarm-up

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?

  1. O(1)
  2. O(log n)
  3. O(n)
  4. O(n log n)

Interview

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?

  1. n
  2. n − 1
  3. n + 1
  4. √n

Interview

Show the answer and solution

Answer: n

ddof = 0 by default; pass ddof=1 for the sample SD.

21Python: print(-7 // 2)

  1. -3
  2. -4
  3. -3.5
  4. 3

Interview

Show the answer and solution

Answer: -4

−3.5 floors to −4.

22The best possible worst-case cost of a comparison sort is…

  1. O(n)
  2. O(n log n)
  3. O(log n)
  4. O(n²)

Interview

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])))

  1. 6
  2. 3
  3. 2
  4. 1

Interview

Show the answer and solution

Answer: 3

The set is {1, 2, 3}.

24Python: x = (1); print(type(x).__name__)

  1. tuple
  2. int
  3. list
  4. Error

Interview

Show the answer and solution

Answer: int

You need a trailing comma: (1,) is a tuple.

25Python: what does print(-17 // 4) show?

Python arithmeticInterview

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?

Python arithmeticInterview

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?

BitsInterview

Show the answer and solution

Answer: 2

20 = 0b10100, which has 2 ones.

28Python: what does print(-13 // 4) show?

Python arithmeticInterview

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?

Python arithmeticInterview

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?

BitsInterview

Show the answer and solution

Answer: 2

40 = 0b101000, which has 2 ones.

31Python: what does print(-9 // 4) show?

Python arithmeticInterview

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?

Python arithmeticInterview

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.