Compute max-ons and deletion indices
Company: J.P. Morgan
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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
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.
Hints
- Try computing the number of `1`s in one window of size `k`, then slide the window one step at a time.
- 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
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`.
Hints
- Deleting index `i` works only if the prefix before `i` matches and the suffix after `i` also matches.
- Precompute which prefixes match and which suffixes match so each index can be checked in O(1).