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)- A. 165
- B. 122
- C. 120
- D. 121
Correct answer: C. 120
The loops enumerate each triple i < j < k exactly once, so c = C(n, 3). Output: 120.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Over all left rotations of [5, 1, 5, 3, 9, 4], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Items K, L, M, N, O, P are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Keys 121, 29, 37, 91, 13, 33, 39 are inserted in this order into an empty table of size 13 by double hashing: h1(k) = k mod 13, h2(k) = 7…
- Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed? python def delalt(h): p = h while…
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- Lines of heights [9, 5, 3, 2, 4, 9, 1, 2, 9] stand at x = 0, 1, …, 8. Two pointers find the pair of lines that, with the x-axis, holds the…