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?
- A. 10
- B. 4
- C. 21
- 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
- Problem: How many integers in [1, 100] have exactly two prime factors counted with multiplicity? Run the code. python def omega(n): c = 0…
- Problem: What is the sum of all proper divisors of 220 (divisors smaller than 220)? Run the code. python print(sum(d for d in range(1…
- Problem: What is the minimum number of coins from {1, 6, 10} that sum to 37? Run the DP. python INF = 10 9 d = [0] + [INF] 37 for v in…
- Problem: How many distinct arrangements of the letters of 'MISSISSIPPI' exist? Run the code. python import math print(math.factorial(11)…
- Problem: What is the maximum total of two non-overlapping subarrays of length 2 in [1, 5, 2, 8, 3]? Run the code. python a = [1, 5, 2, 8…
- Problem: How many integers in [1, 1000] have digit sum equal to 5 and are divisible by 5? Run the code. python print(sum(1 for x in…