Data Structures & AlgorithmsHardTCS NQT · Product Company OA

Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] a[j] at most 10? Run the code.

Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] * a[j] at most 10? Run the code.
a = [1, 2, 3, 4, 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?
  1. A. 7
  2. B. 6
  3. C. 14
  4. D. 10

Correct answer: A. 7

The pairs with product ≤ 10 are (1,2), (1,3), (1,4), (1,5), (2,3), (2,4) and (2,5); (3,4) gives 12. Using < 10 drops (2,5) (6), counting ordered pairs gives 14, and starting j at i adds (1,1), (2,2), (3,3) (10).

More Coding Interview Problems questions

Exams that ask this topic