Data Structures & AlgorithmsModerateTCS NQT · Product Company OA

Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code.

Problem: How many distinct values appear among the minima of all subarrays of [3, 1, 2]? Run the code.
a = [3, 1, 2]
mins = {min(a[i:j]) for i in range(len(a)) for j in range(i + 1, len(a) + 1)}
print(len(mins))
What is printed?
  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 6

Correct answer: B. 3

The minima are 3, 1 and 2, so there are 3 distinct values even though 6 subarrays exist.

More Coding Interview Problems questions

Exams that ask this topic