Data Structures & AlgorithmsModerateCAT · TCS NQT
Problem: How many unordered pairs {x, y} with x ≠ y from the set {1, 2, 3, 4, 5} have x + y even?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Two numbers have an even sum exactly when they share parity. The odd numbers 1, 3, 5 give three pairs and the even numbers 2, 4 give one pair, totalling four.
More Coding Interview Problems questions
- Problem: How many binary strings of length 4 contain no two adjacent 1s?
- Problem: How many two-digit numbers (10–99) have digits that sum to exactly 9?
- Problem: The undirected graph on vertices 0, 1, 2 has edges 0-1 (5), 1-2 (5) and 0-2 (12). What is the shortest path length from 0 to 2?
- Problem: A complete binary tree has all levels full with depths 0 to 4 (root at depth 0). How many nodes does it have in total?
- Compute the sum of gcd(i, j) over all pairs 1 ≤ i < j ≤ 50. python from math import gcd print(sum(gcd(i,j) for i in range(1,51) for j in…
- How many integers n with 1 ≤ n ≤ 10000 have exactly four positive divisors? Count them with a divisor-count sieve. python N=10000…