Data Structures & AlgorithmsVery hardProduct company OA
What is the length of the longest contiguous subarray of [0, 1, 1, 0, 1, 1, 1, 0, 0, 0] with an equal number of 0s and 1s? (Map 0 to −1 and look for repeated prefix sums.)
- A. 8
- B. 6
- C. 4
- D. 10
Correct answer: D. 10
With 0 → −1 the prefix sums are −1, 0, 1, 0, 1, 2, 3, 2, 1, 0. The sum 0 first occurs at the start (index −1) and last at index 9, so the WHOLE array has five 0s and five 1s: length 10. Storing the LAST occurrence of each sum instead of the first finds only shorter windows.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the maximum sum of a non-empty contiguous subarray of the CIRCULAR array [5, −8, 3, 4, −2, 6, −9, 7] (a subarray may wrap from the…
- What is the maximum product of a non-empty contiguous subarray of [2, −3, 0, −2, −4, 3, −1]?
- What is the next lexicographic permutation of [1, 5, 8, 4, 7, 6, 5, 3, 1]?
- Circular route: station i gives gas[i] and driving to station i + 1 costs cost[i], with gas = [3, 1, 5, 2, 6] and cost = [4, 3, 2, 4, 2]…
- A monotonic stack removes duplicate letters so that each letter appears exactly once and the result is the lexicographically SMALLEST…
- 20 keys are hashed independently and uniformly into 100 slots. What is the EXPECTED number of colliding pairs (unordered pairs of keys…