Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.
- A. 15
- B. 11
- C. 6
- D. 16
Correct answer: D. 16
Map 1 to +1 and 0 to -1. A balanced substring is a pair of equal prefix balances, so count equal pairs with a hash map seeded with balance 0. Forgetting that seed loses substrings that start at index 0.
More Coding Interview Problems questions
- Array a = [2, 7, -3, 5, 11, 4, 9, -1, 6]. How many index pairs (i, j) with i < j have a[i] + a[j] = 10?
- Array a = [2, 1, -1, 4, -2, 3, 0, 1, 1, 0, 2]. How many indices i have the sum of the elements strictly left of i equal to the sum of the…
- Array a = [3, 1, 2, 6, 4, 5, 9]. How many index pairs (i < j) satisfy a[i] mod 3 = a[j] mod 3?
- Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?
- For the positive array a = [2, 3, 1, 2, 4, 3] and S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Sorted array a = [1, 3, 4, 7, 10, 12, 15]. How many pairs of indices i < j satisfy a[j] - a[i] <= 3?