Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code.
Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code.
print(sum(1 for x in range(1, 501) if any(k * k == x for k in range(1, 23)) or x % 7 == 0))What is printed?
- A. 93
- B. 90
- C. 71
- D. 87
Correct answer: B. 90
22 squares + 71 multiples of 7 − 3 overlaps (49, 196, 441) = 90. Not removing the overlap gives 93, 71 counts only multiples of 7, and subtracting the overlap twice gives 87.
More Coding Interview Problems questions
- Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code…
- Problem: What is the number of paths from (1,1) to (3,3) moving only right or up in a grid with all cells free? Run the DP. python g =…
- Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code. python print(len({a ^ b for a in range(4) for…
- Problem: What is the sum of all integers in [1, 200] that are both odd and multiples of 5? Run the code. python print(sum(x for x in…
- Problem: What is the maximum number of distinct characters in a substring of length 4 of 'abcabcabc'? Run the code. python s = 'abcabcabc'…
- Problem: How many ways are there to tile a 2 x 6 board with 2 x 1 dominoes? Run the DP. python f = [1, 1] for n in range(2, 7)…