Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Max Consecutive Ones With Flips

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Max Consecutive Ones With Flips. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

SnapLogic Pipeline executions may contain binary status markers, where 1 means a successful step and 0 means a recoverable failure. Given the markers and a replacement budget k, find the longest contiguous segment that can become all 1s by replacing at most k zeros.

Return the segment boundaries as [left, right]. If multiple segments have the same maximum length, return the one with the smallest left index. Indices are zero-based. If the input is empty, return [-1, -1].

Formal Specification

Implement longest_ones_window(nums, k), where nums is a list of integers containing only 0 and 1, and k is a nonnegative integer. Return a two-element list [left, right] describing the selected inclusive window. The window must contain at most k zeros, and its length must be maximal.

Constraints

  • 0 <= len(nums) <= 10^6
  • nums[i] is either 0 or 1
  • 0 <= k <= 10^6
  • The answer uses at most k replacements
  • Return the earliest window when multiple maximum-length windows exist

Function Signature

def longest_ones_window(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output