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?
- A. 55
- B. 45
- C. 121
- 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
- Problem: What is the sum of the ASCII codes of the characters of 'hello'? Run the code. python print(sum(ord(c) for c in 'hello')) What is…
- Problem: What is the length of the longest strictly increasing contiguous subarray of [3, 4, 5, 1, 2, 3, 4, 5, 6]? Run the code. python a…
- Problem: What is the minimum number of cuts needed to partition 'abcde' into palindromes? Run the DP. python s = 'abcde' n = len(s) pal =…
- Problem: How many two-digit numbers have a digit sum that is a prime number? Run the code. python def isp(n): return n > 1 and all(n % d…
- Problem: How many subarrays of [1, 2, 2, 3, 3, 3] have all equal maximum and minimum values? Run the code. python a = [1, 2, 2, 3, 3, 3]…
- Problem: How many integers in [1, 100] have exactly two prime factors counted with multiplicity? Run the code. python def omega(n): c = 0…