Data Structures & AlgorithmsModerateTCS NQT Digital · counting
Problem: How many unordered pairs of indices (i < j) of [5, 5, 5, 5] have a sum of 10? Run the code.
Problem: How many unordered pairs of indices (i < j) of [5, 5, 5, 5] have a sum of 10? Run the code.
a = [5, 5, 5, 5] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] + a[j] == 10))What is printed?
- A. 6
- B. 3
- C. 10
- D. 12
Correct answer: A. 6
Any two of the four equal values sum to 10, so the count is C(4,2) = 6. Checking only adjacent pairs gives 3, starting j at i also pairs each element with itself (10), and counting ordered pairs gives 12.
More Coding Interview Problems questions
- Problem: What does the following print? python print(list(map(lambda x: x 2, filter(lambda x: x % 2, range(6))))) What is printed?
- Problem: What does the following print? python print(bool([]), bool([0]), bool(0.0), bool('False')) What is printed?
- Problem: What does the following print? python a = (1,) print(a 3, len(a 0)) What is printed?
- Problem: What does the following print? python print(list('abc')[::-1] == ['c', 'b', 'a'], 'abc'.index('c')) What is printed?
- Problem: What does the following print? python x = 5 x <<= 2 x |= 1 print(x) What is printed?
- Problem: What does the following print? python print(any([]), all([])) What is printed?