Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: In the binary array [1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1] at most 2 zeros may be flipped to 1. What is the length of the longest run of 1s that can be obtained?
- A. 7
- B. 8
- C. 11
- D. 3
Correct answer: B. 8
Zeros sit at indices 2, 3 and 7; the best window flips 3 and 7 and spans indices 3..10, length 8. Allowing only one flip gives 7, three flips gives 11, and 3 is the longest existing run without flipping.
More Coding Interview Problems questions
- Problem: Starting from the empty set, a process inserts the numbers 5, 3, 8, 1, 4, 7 into a binary search tree, and then deletes nothing…
- Problem: The array [4, 2, 5, 1, 3] is turned into a max-heap by sift-down from the last parent to the root (standard heapify, 0-indexed)…
- Problem: A string is "racecar" and we ask: how many substrings of length 2 or 3 are palindromes (counted by index ranges)?
- Problem: The array [6, 1, 9, 4, 4, 8] is scanned, and for each index the number of earlier elements that are strictly smaller is recorded…
- Problem: The array [1, 5, 2, 6, 3, 7, 4, 8] is split into two halves, [1, 5, 2, 6] and [3, 7, 4, 8]. The result is formed by interleaving…
- Problem: How many integers from 1 to 1000 are perfect squares or perfect cubes (counted once even if both)?