Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code.
Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code.
print(sum(1 for i in range(1, 31) for j in range(i + 1, 31) if (i + j) % 6 == 0))What is printed?
- A. 70
- B. 140
- C. 80
- D. 72
Correct answer: A. 70
Residues mod 6 each occur 5 times: (0,0) and (3,3) give C(5,2) = 10 each and (1,5), (2,4) give 25 each, total 70. Ordered pairs give 140, allowing i = j adds 10 for 80, and 'about one sixth of 435 pairs' gives 72.
More Coding Interview Problems questions
- Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code. python print(sum(1 for x in range(1, 10001) if x…
- Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code. python n = 1000 c = 0 p = 2 while p <= n: c += n // p p…
- Problem: How many distinct values does (i j) mod 7 take for 1 <= i, j <= 7? Run the code. python print(len({(i j) % 7 for i in range(1, 8)…
- Problem: What is the maximum sum of non-adjacent elements of the circular array [3, 2, 7, 10]? Run the code. python def lin(a): inc = exc…
- Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code. python from itertools import product…
- Problem: In how many ways can you climb 12 stairs taking 1, 2 or 3 steps at a time (order matters)? Run the DP. python ways = [1] + [0] 12…