Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Array a = [1, -2, 3, 2, -1, 4, -3, 2]. What is the length of the longest contiguous subarray whose sum is exactly 4?
- A. 7
- B. 1
- C. 5
- D. 8
Correct answer: A. 7
Store the FIRST index of each prefix sum; at j look up prefix − 4. The longest hit is indices 0..6, [1, −2, 3, 2, −1, 4, −3], with sum 4 and length 7. The shortest such subarray is [4] (1), there are 5 such subarrays in total, and 8 would need the whole array, whose sum is 6.
More Coding Interview Problems questions
- Array a = [3, -5, 2, -1, -4, 2, 1, -6, 2]. How many contiguous subarrays of length exactly 4 have a negative sum?
- Sorted array [1, 2, 2, 3, 5, 5, 6, 8]. How many index pairs i < j have 6 <= a[i] + a[j] <= 8?
- How many non-empty subsets of {1, 2, 3, 4, 5, 6, 7} have a bitwise AND of their elements equal to 0?
- Lists L1 = {1, 4, 7}, L2 = {2, 5, 8} and L3 = {3, 6, 9}. Choosing one element from each list, how many choices give a total of 15?
- From the values [12, 3, 9, 15, 6, 20, 10, 7], choose 3 of them. What is the smallest possible difference between the largest and the…
- Meetings (start, end) are (1,2), (3,4), (0,6), (5,7), (8,9), (5,9). A room hosts one meeting at a time, and a meeting may start exactly…