Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many pairs (x, y) with x <= y and x + y = 12 lie in 1..11? Run the code.

Problem: How many pairs (x, y) with x <= y and x + y = 12 lie in 1..11? Run the code.
print(sum(1 for x in range(1, 12) for y in range(1, 12) if x + y == 12 and x <= y))
What is printed?
  1. A. 6
  2. B. 5
  3. C. 7
  4. D. 11

Correct answer: A. 6

x ranges over 1..6 and y = 12 - x stays within 1..11, giving 6 pairs.

More Coding Interview Problems questions

Exams that ask this topic