Longest Subarray of 1's After Deleting One Element
A medium-tier problem at 69% community acceptance, tagged with Array, Dynamic Programming, Sliding Window. Reported in interviews at Yandex and 2 others.
You've got an array of 1s and 0s, you delete exactly one element, and you need to find the longest consecutive 1s that remain. It sounds simple until you hit it live and realize there's a trap in the obvious approach. Yandex, Tinkoff, and VK have all asked this. The 69% acceptance rate means most candidates solve it, but the ones who don't usually froze on whether they're allowed to skip the deletion or got tangled in state tracking. StealthCoder surfaces a clean solution in seconds if you blank on the pattern during your assessment.
Companies that ask "Longest Subarray of 1's After Deleting One Element"
Longest Subarray of 1's After Deleting One Element is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.
Get StealthCoderThe trap: you can't just find the longest run of 1s and delete one 0 from it, because what if there's no 0 to delete, or what if the optimal deletion is a 1 that bridges two runs. The real pattern is sliding window with state: track how many 0s you've seen in your current window. When you hit a second 0, contract the left boundary. The delete happens implicitly when you shrink the window back down. Dynamic programming also works (left array for max run ending at i, right array for max run starting at i, then check each position as a deletion point), but sliding window is faster in practice and doesn't require the extra space. Common miss: forgetting that the deletion is mandatory, so if your window is all 1s, you still subtract 1 from the final answer. The trick is tracking your 0 count, not your 1 count.
Pattern tags
You know the problem.
Make sure you actually pass it.
Longest Subarray of 1's After Deleting One Element recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Subarray of 1's After Deleting One Element interview FAQ
Why is the greedy approach of 'find longest run and delete the 0 in it' wrong?+
Because the optimal deletion might not be a 0 inside that run. It might be a 1 that joins two runs separated by one 0. Sliding window or two-pass DP catches all cases. Greedy misses the global picture.
Is this still being asked at these companies?+
Yes. Yandex, Tinkoff, and VK all report it. It's a medium-difficulty classic. If you're targeting any of them, it's worth the rehearsal.
What's the state tracking trick everyone gets wrong?+
They count 1s instead of 0s. You want one 0 in your window, so track 0 count. When it hits 2, shrink from the left. The math is cleaner and the intent is clearer.
Does the deletion have to happen, or is it optional?+
Mandatory. You always delete exactly one element. If your answer before subtracting 1 is the window size, the final answer is size minus 1. Don't forget this.
How does this differ from max consecutive 1s without deletion?+
Without deletion, you're just tracking runs of 1s. With deletion, you're allowed one 'free pass' through one element of any type. That one-element allowance is what makes sliding window the natural fit here, and why a DP approach needs two passes.
Want the actual problem statement? View "Longest Subarray of 1's After Deleting One Element" on LeetCode →