Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code.
Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code.
print(sum(x for x in range(1, 200) if x % 3 != 0 and sum(1 for y in range(1, x + 1) if y % 3 != 0) <= 100))What is printed?
- A. 3367
- B. 5050
- C. 11325
- D. 7500
Correct answer: D. 7500
The first 100 non-multiples of 3 are 1..149 minus 3, 6, …, 147: 11175 − 3675 = 7500. Stopping at 100 gives 3367, 5050 sums 1..100, and 11325 sums 1..150 without removing anything.
More Coding Interview Problems questions
- 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)…
- Problem: What is the length of the longest common subsequence of 'abcdef' and 'acebdf'? Run the DP. python a, b = 'abcdef', 'acebdf' dp =…
- Problem: What is the number of subsets of {1, 2, ..., 8} whose elements sum to a multiple of 8? Run the code. python print(sum(1 for m in…