Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5, 7] have an odd product?
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5, 7] have an odd product?
a = [1, 2, 3, 4, 5, 7] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] * a[j] % 2 == 1))What is printed?
- A. 6
- B. 9
- C. 12
- D. 4
Correct answer: A. 6
A product is odd only when both factors are odd; the 4 odd values 1, 3, 5, 7 give C(4,2) = 6 pairs. Counting even products gives 15 − 6 = 9, counting ordered pairs gives 12, and counting odd elements instead of pairs gives 4.
More Coding Interview Problems questions
- Problem: What is the total number of set bits in the binary representations of 1 through 100? python print(sum(bin(i).count('1') for i in…
- Problem: What is the minimum number of jumps needed to reach the last index of [1, 2, 1, 1, 1] when a[i] is the maximum jump length from…
- Problem: What is the number of trailing zeros of 1000!? python n = 1000 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c) What is…
- Problem: How many 1-bits are set in 2^20 - 1? python print(bin(2 20 - 1).count('1')) What is printed?
- Problem: What is the maximum sum of a contiguous subarray of exactly length 3 in [5, -2, 7, 1, 0, 4]? python a = [5, -2, 7, 1, 0, 4]…
- Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers? python from itertools import combinations print(sum(1…