PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array algorithm skills, specifically recognition of sliding-window or two-pointer techniques and reasoning about time and space efficiency for contiguous subarray problems.

  • hard
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Find Longest Activatable Server Streak

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

You are given a binary array `serverStates`, where `1` means a server is currently on and `0` means it is off. You may turn on at most `k` currently off servers. Return the maximum possible length of a contiguous block of servers that can all be on after performing at most `k` such changes. Example: - Input: `serverStates = [1, 0, 0, 1, 1, 0, 1]`, `k = 2` - Output: `5` Explanation: By turning on two of the `0` values, you can create a longest contiguous segment of 5 on servers. Write an efficient function to compute the answer for large arrays.

Quick Answer: This question evaluates array algorithm skills, specifically recognition of sliding-window or two-pointer techniques and reasoning about time and space efficiency for contiguous subarray problems.

You are given a binary array `serverStates`, where `1` means a server is currently on and `0` means it is off. You may turn on at most `k` currently off servers by changing up to `k` zeros into ones. Return the maximum possible length of a contiguous block of servers that can all be on after performing at most `k` such changes. If the array is empty, return `0`.

Constraints

  • 0 <= len(serverStates) <= 200000
  • serverStates[i] is either 0 or 1
  • 0 <= k <= len(serverStates)

Examples

Input: ([1, 0, 0, 1, 1, 0, 1], 2)

Expected Output: 5

Explanation: By turning on two off servers, the longest contiguous all-on segment has length 5.

Input: ([1, 1, 1, 1], 1)

Expected Output: 4

Explanation: All servers are already on, so the full array is the answer.

Input: ([], 3)

Expected Output: 0

Explanation: There are no servers, so the longest streak is 0.

Input: ([0, 0, 1, 1, 0, 1, 1, 1, 0], 0)

Expected Output: 3

Explanation: No changes are allowed, so we take the longest existing run of ones, which is length 3.

Input: ([0, 0, 0], 5)

Expected Output: 3

Explanation: You can turn on all off servers, so the whole array becomes one contiguous streak of length 3.

Input: ([0], 1)

Expected Output: 1

Explanation: Turning on the single server creates a streak of length 1.

Hints

  1. Checking every possible subarray is too slow for large inputs. Try maintaining a moving window instead.
  2. Keep track of how many zeros are inside the current window. If that count becomes greater than `k`, move the left side of the window forward until it becomes valid again.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
  • Find the Most Common Visit Pattern - Amazon (hard)
  • Maximize Value Under a Budget - Amazon (medium)