Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft

What does this loop-counting code print? (Python)

What does this loop-counting code print? (Python)
n, c = 10, 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            c += 1
print(c)
  1. A. 165
  2. B. 122
  3. C. 120
  4. D. 121

Correct answer: C. 120

The loops enumerate each triple i < j < k exactly once, so c = C(n, 3). Output: 120.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic