Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array [5, -5, 2, 3, -2, 7, 3]. How many contiguous subarrays have a sum that is 0, 5 or 10?
- A. 11
- B. 10
- C. 8
- D. 5
Correct answer: B. 10
Of the 28 subarray sums, 2 equal 0, 5 equal 5 and 3 equal 10: 10 in total (a prefix-sum hash map counts each target in O(n)). Counting every multiple of 5 also catches the sum −5 (11), forgetting the zero-sum subarrays gives 8, and counting only sum 5 gives 5.
More Coding Interview Problems questions
- Sorted positive array [1, 3, 4, 6, 9, 12]. How many index pairs i < j have a[i] a[j] >= 18?
- How many integers x with 1 <= x <= 64 have a binary representation (without leading zeros) that reads the same forwards and backwards?
- Problem: The suffix array of a string is its suffix start indices sorted by the suffix text. What is the value at position 2 (0-based) for…
- Problem: How many topological orders does the DAG with edges 0->2, 1->2, 2->3 on vertices 0..3 have? Run the brute-force code. python from…
- Problem: A row of 5 cells starts as [0, 0, 1, 0, 0]. Each step, every cell becomes the XOR of its two neighbours (cells outside the row…
- Problem: What is the exponent of the prime 3 in 100!? Use Legendre's formula, summing 100 // 3^k. Run the code. python print(sum(100 // 3…