The base problem I got pretty fast, sliding window with a counter tracking how many zeros are inside the window.
Start by clarifying the problem and edge cases, then present a sliding window solution that tracks the last zero position to compute the maximum run of 1s with at most one flip. For the streaming variant, explain how to maintain the same logic in O(1) space by keeping only the necessary state variables.
Pro tip: Emphasize that the streaming solution is essentially the same sliding window but without storing the array, and discuss how you would handle the flip count if it were generalized to k flips.
Confirm that the array contains only 0s and 1s, that you can flip at most one 0, and that you need the length of the longest contiguous run of 1s after the flip. Discuss edge cases like all 1s, all 0s, and empty array.
Explain that you can use a sliding window that allows at most one zero. Maintain the window boundaries and the index of the last zero seen. When a second zero is encountered, move the left boundary to just after the previous zero.
Walk through the algorithm step by step with a small example, showing how the window expands and contracts, and how the maximum length is updated. Highlight that the window always contains at most one zero.
Explain that for a stream, you don't need to store the array; you only need to keep track of the current window length, the position of the last zero, and the maximum length seen so far. This uses O(1) space.
State that both solutions run in O(n) time and O(1) space. Mention that the streaming solution is more memory-efficient and suitable for large or infinite streams, but requires careful handling of state.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.