Data Structures & AlgorithmsHardTCS NQT Digital · counting
Problem: How many ordered pairs (i, j) with i != j of [1, 2, 3, 4] have an even sum? Run the code.
Problem: How many ordered pairs (i, j) with i != j of [1, 2, 3, 4] have an even sum? Run the code.
a = [1, 2, 3, 4] print(sum(1 for i in range(4) for j in range(4) if i != j and (a[i] + a[j]) % 2 == 0))What is printed?
- A. 2
- B. 4
- C. 6
- D. 8
Correct answer: B. 4
The unordered even-sum pairs are (1,3) and (2,4), and each gives 2 ordered pairs, for 4 in total.
More Coding Interview Problems questions
- Problem: What does the following print? python from collections import Counter print(Counter('mississippi').mostcommon(1)) What is printed?
- Problem: What does the following print? python print(round(1.23456, 2), round(-2.5)) What is printed?
- Problem: What does the following print? python print(max([], default=-1), sum([]), divmod(-7, 3))
- Problem: What does the following print? python print(2 -1, 7 % -3)
- Problem: What does the following print? python print(sorted([3, 1, 2], key=lambda x: -x)[0]) What is printed?
- Problem: Starting from 3, you may apply two operations: "+1" or "×2". What is the minimum number of operations needed to reach exactly 50?