Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
For the string s = "aabbabbaab", find the length of the longest substring with exactly as many letter a as letter b.
- A. 8
- B. 10
- C. 4
- D. 16
Correct answer: B. 10
Map a → +1, b → −1 and store the FIRST index of each prefix balance, seeded with balance 0 at index −1; here the whole string has 5 a's and 5 b's, so the answer is 10. Forgetting the seed misses prefixes that start at index 0 (8), overwriting with the latest index gives 4, and counting balanced substrings instead of measuring them gives 16.
More Coding Interview Problems questions
- For the array a = [4, 1, 7, 2, 2, 5, 3, 6], how many contiguous non-empty subarrays have XOR equal to 0? Use prefix XOR values.
- Array a = [3, 1, 4, 1, 5, 9, 2, 6]. How many subarrays of length exactly 3 have an even sum?
- Array a = [2, 4, 6, 1, 3, 5, 8, 7]. How many contiguous subarrays of length at least 2 have a sum divisible by 4?
- From the sorted side lengths [4, 6, 7, 9, 10, 13, 15], how many different triples (choosing distinct positions) can form a non-degenerate…
- Sorted arrays A = [1, 7, 12, 20, 31] and B = [3, 9, 15, 25, 40]. What is the smallest value of |x - y| over x in A and y in B?
- Values [3, 9, 4, 15, 8, 2, 11, 6] are given. Remove the fewest values so that the maximum minus the minimum of the remaining values is at…