Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many subarrays of [1, 2, 3, 1, 2, 3, 4] contain exactly 3 distinct values? Run the brute-force code.
Problem: How many subarrays of [1, 2, 3, 1, 2, 3, 4] contain exactly 3 distinct values? Run the brute-force code.
a = [1, 2, 3, 1, 2, 3, 4] print(sum(1 for i in range(len(a)) for j in range(i, len(a)) if len(set(a[i:j + 1])) == 3))What is printed?
- A. 11
- B. 24
- C. 15
- D. 5
Correct answer: A. 11
Exactly 3 distinct values: 11 subarrays. 'At most 3' gives 24, 'at least 3' gives 15, and length exactly 3 gives 5; the sliding-window trick computes at-most(3) − at-most(2) = 24 − 13 = 11.
More Coding Interview Problems questions
- Problem: How many integers in [1, 1000] are divisible by 7, 11 or 13? Run the code. python print(sum(1 for x in range(1, 1001) if x % 7 ==…
- Problem: What is the number of trailing zeros in 2025! (in decimal)? Run the code. python n = 2025 c = 0 p = 5 while p <= n: c += n // p p…
- Problem: What is the sum of all distinct prime factors of 2025? Run the code. python n = 2025 f = 2 ps = set() while f f <= n: while n % f…
- Problem: How many integers x in [1, 500] have x^2 + x + 41 prime? Run the code. python def isp(n): return n > 1 and all(n % d for d in…
- Problem: What is the length of the longest subarray of [2, 4, 6, 3, 8, 10, 12] whose elements are all even? Run the code. python a = [2…
- Problem: How many distinct values does floor(100 / i) take for i = 1 to 100? Run the code. python print(len({100 // i for i in range(1…