Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many ways can you pick 2 different integers from [1, 20] whose sum is divisible by 4? Run the code.
Problem: How many ways can you pick 2 different integers from [1, 20] whose sum is divisible by 4? Run the code.
print(sum(1 for i in range(1, 21) for j in range(i + 1, 21) if (i + j) % 4 == 0))What is printed?
- A. 90
- B. 45
- C. 55
- D. 47
Correct answer: B. 45
Residues 0 and 2 pair with themselves (C(5,2) = 10 each), and 1 pairs with 3 (5 · 5 = 25): 45. Ordered pairs give 90, allowing i = j gives 55, and 190/4 gives about 47.
More Coding Interview Problems questions
- Problem: What is the count of 3-digit numbers whose digits are all distinct and in strictly increasing order? Run the code. python…
- Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code. python print(sum(x for x in…
- Problem: How many distinct values does x mod 12 take over the numbers x in [1, 1000] that are not divisible by 4? Run the code. python…
- Problem: What is the length of the longest palindromic substring of 'abacdcaba'? Run the code. python s = 'abacdcaba' best = 0 for c in…
- Problem: How many integers in [1, 10000] have a digit sum that is divisible by 7? Run the code. python print(sum(1 for x in range(1…
- Problem: How many integer solutions (a, b) with 1 <= a <= b <= 20 satisfy a b = 36? Run the code. python print(sum(1 for a in range(1, 21)…