Data Structures & AlgorithmsModerateTCS NQT · basics
Problem: How many integers in [1, 50] are divisible by both 2 and 3? Run the code.
Problem: How many integers in [1, 50] are divisible by both 2 and 3? Run the code.
print(sum(1 for x in range(1, 51) if x % 2 == 0 and x % 3 == 0))What is printed?
- A. 16
- B. 8
- C. 25
- D. 6
Correct answer: B. 8
Being divisible by both 2 and 3 means divisible by 6, giving 6, 12, ..., 48 which is 8 numbers.
More Coding Interview Problems questions
- Problem: What is the sum of the first 10 odd positive integers? Run the code. python print(sum(2 i + 1 for i in range(10))) What is printed?
- Problem: How many 2-digit numbers have both digits odd? Run the code. python print(sum(1 for x in range(10, 100) if int(str(x)[0]) % 2 ==…
- Problem: How many pairs (i < j) of [1, 3, 5, 7, 9] have a[i] + a[j] >= 10? Run the code. python a = [1, 3, 5, 7, 9] print(sum(1 for i in…
- Problem: What is the maximum sum of any 3 consecutive elements of [1, -2, 3, 4, -1]? Run the code. python a = [1, -2, 3, 4, -1]…
- Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code. python sq = {x x for x in range(1, 11)} cu = {x…
- Problem: What is the sum of the digits of 2^10 = 1024? Run the code. python print(sum(int(c) for c in str(2 10))) What is printed?