Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many subarrays of [1, 2, 2, 3, 3, 3] have all equal maximum and minimum values? Run the code.

Problem: How many subarrays of [1, 2, 2, 3, 3, 3] have all equal maximum and minimum values? Run the code.
a = [1, 2, 2, 3, 3, 3]
print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a) + 1) if max(a[i:j]) == min(a[i:j])))
What is printed?
  1. A. 10
  2. B. 4
  3. C. 21
  4. D. 6

Correct answer: A. 10

Only constant subarrays qualify: runs of length 1, 2, 3 give 1 + 3 + 6 = 10. Requiring length ≥ 2 gives 4, 21 counts all subarrays, and 6 counts only the single elements.

More Coding Interview Problems questions

Exams that ask this topic