PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in array and string algorithms, specifically sliding-window and circular indexing for maximizing contiguous on-states and string comparison for identifying deletion indices, emphasizing correctness in index arithmetic and edge-case handling.

  • Medium
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

Compute max-ons and deletion indices

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question Given a circular array of n computers represented by 0 (off) and 1 (on), determine the maximum possible number of on computers within any contiguous block of k adjacent computers. Given two strings s1 and s2 where |s1| = |s2| + 1 and s2 can be obtained by deleting exactly one character from s1, return all indices in s1 whose removal yields s2.

Quick Answer: This question evaluates proficiency in array and string algorithms, specifically sliding-window and circular indexing for maximizing contiguous on-states and string comparison for identifying deletion indices, emphasizing correctness in index arithmetic and edge-case handling.

Part 1: Maximum On Computers in a Circular Block

You are given a circular array `computers` of length `n`, where each element is either `0` (off) or `1` (on). Because the array is circular, the element after the last one is the first element again. Find the maximum number of on computers that can appear in any contiguous block of exactly `k` adjacent computers.

Constraints

  • 1 <= len(computers) <= 200000
  • Each value in `computers` is either 0 or 1
  • 1 <= k <= len(computers)

Examples

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

Expected Output: 2

Explanation: The best length-3 circular blocks contain two `1`s, such as [1, 0, 1] or [0, 1, 1].

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

Expected Output: 3

Explanation: A wrap-around block [0, 1, 1, 1] contains three on computers.

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

Expected Output: 0

Explanation: Every block contains only off computers.

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

Expected Output: 3

Explanation: When `k` equals the array length, every window covers all computers, so the answer is the total number of `1`s.

Input: ([1], 1)

Expected Output: 1

Explanation: Single-element edge case.

Solution

def solution(computers, k):
    n = len(computers)
    if n == 0 or k == 0:
        return 0

    window = sum(computers[:k])
    best = window

    for start in range(1, n):
        window -= computers[start - 1]
        window += computers[(start + k - 1) % n]
        if window > best:
            best = window

    return best

Time complexity: O(n). Space complexity: O(1).

Hints

  1. Try computing the number of `1`s in one window of size `k`, then slide the window one step at a time.
  2. Because the array is circular, use modulo indexing when adding the new element that enters the window.

Part 2: Indices Whose Deletion Produces the Target String

You are given two strings `s1` and `s2` such that `len(s1) = len(s2) + 1`. Find all indices `i` in `s1` where deleting the character `s1[i]` makes the remaining string exactly equal to `s2`. Return the indices in increasing order.

Constraints

  • 1 <= len(s1) <= 200000
  • len(s2) = len(s1) - 1
  • `s1` and `s2` consist of lowercase English letters

Examples

Input: ("abc", "ac")

Expected Output: [1]

Explanation: Removing `b` at index 1 gives `ac`.

Input: ("aab", "ab")

Expected Output: [0, 1]

Explanation: Removing either the first or second `a` produces `ab`.

Input: ("aaaa", "aaa")

Expected Output: [0, 1, 2, 3]

Explanation: Deleting any one `a` still leaves `aaa`.

Input: ("abca", "aba")

Expected Output: [2]

Explanation: Only removing `c` at index 2 yields `aba`.

Input: ("a", "")

Expected Output: [0]

Explanation: Deleting the only character produces the empty string.

Solution

def solution(s1, s2):
    n = len(s1)
    m = len(s2)

    if n != m + 1:
        return []

    prefix = [False] * (m + 1)
    prefix[0] = True
    for i in range(m):
        prefix[i + 1] = prefix[i] and (s1[i] == s2[i])

    suffix = [False] * (m + 1)
    suffix[m] = True
    for i in range(m - 1, -1, -1):
        suffix[i] = suffix[i + 1] and (s1[i + 1] == s2[i])

    ans = []
    for i in range(n):
        if prefix[i] and suffix[i]:
            ans.append(i)

    return ans

Time complexity: O(n). Space complexity: O(n).

Hints

  1. Deleting index `i` works only if the prefix before `i` matches and the suffix after `i` also matches.
  2. Precompute which prefixes match and which suffixes match so each index can be checked in O(1).
Last updated: May 5, 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 Integer Square Root - J.P. Morgan (medium)
  • Implement KNN from Scratch - J.P. Morgan (medium)
  • Minimize Operations to Balance Shipments - J.P. Morgan (medium)
  • Solve scheduling and collision problems - J.P. Morgan (medium)
  • Design an autocomplete suggestions API - J.P. Morgan (easy)