Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: Count the subarrays of [2, 1, 3, 2, 1, 4, 2] that have exactly 2 distinct values.
- A. 13
- B. 6
- C. 21
- D. 5
Correct answer: B. 6
exactly(2) = atMost(2) − atMost(1) = 13 − 7 = 6: [2,1], [1,3], [3,2], [2,1], [1,4], [4,2]. Counting 'at most 2' gives 13, 'at least 2' gives 21, and counting distinct subarray contents merges the two [2,1] windows (5).
More Coding Interview Problems questions
- Problem: Start at 1 and apply the rules: multiply by 3 if the number is even, add 5 if it is odd. Apply 9 steps. What is the final value?
- Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so…
- Problem: A sequence is defined by a1 = 1, a2 = 1, and an = an−1 + an−2 mod 7 for n ≥ 3. What is a30 (1-indexed)?
- Problem: A grid of size 3×4 has the cell values 1 to 12 filled row-wise. Starting at the top-left (value 1), you may move right or down…
- For a = [2, -1, 4, -3, 5], which of the following statements is TRUE? A. Some contiguous subarray sums to 7. B. Every prefix sum from the…
- What does this program print? python a = [1, -1, 2, -2, 3] seen = {0: 0} s = 0; out = [] for i, x in enumerate(a, 1): s += x if s in seen…