Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many lattice points (x, y) with x >= 0, y >= 0 and x + y <= 10 lie in the triangle? Run the code.

Problem: How many lattice points (x, y) with x >= 0, y >= 0 and x + y <= 10 lie in the triangle? Run the code.
print(sum(1 for x in range(0, 11) for y in range(0, 11) if x + y <= 10))
What is printed?
  1. A. 55
  2. B. 45
  3. C. 121
  4. D. 66

Correct answer: D. 66

Diagonals x + y = k for k = 0..10 hold k + 1 points each: 1 + 2 + … + 11 = 66. Using x + y < 10 gives 55, requiring x, y ≥ 1 gives 45, and 121 counts the whole 11 × 11 square.

More Coding Interview Problems questions

Exams that ask this topic