Compute ring window max and deletion indices
Company: J.P. Morgan
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving across arrays and strings, covering concepts such as sliding-window techniques on circular arrays, wraparound handling, index recovery after single-character deletion, duplicate-character considerations, and time/space complexity analysis within the Coding & Algorithms domain.
Ring Window Maximum
Constraints
- 1 <= n == len(bits) <= 10^5
- Each bits[i] is either 0 or 1
- 1 <= k <= n
- The window wraps around the end of the array (circular)
Examples
Input: ([1, 0, 1, 1, 0], 2)
Expected Output: 2
Explanation: Best window of size 2 is [1, 1] (positions 2-3) with 2 ones.
Input: ([1, 1, 0, 0, 1], 3)
Expected Output: 3
Explanation: Wrapping window covering positions 4, 0, 1 = [1, 1, 1] has 3 ones.
Input: ([0, 0, 0, 0], 2)
Expected Output: 0
Explanation: All computers off, so every window has 0 ones.
Input: ([1, 1, 1, 1], 4)
Expected Output: 4
Explanation: k equals n, so the whole ring is the window: 4 ones.
Input: ([1], 1)
Expected Output: 1
Explanation: Single on computer, single-element window.
Input: ([1, 0, 0, 0, 1, 1], 3)
Expected Output: 3
Explanation: Wrapping window over positions 4, 5, 0 = [1, 1, 1] has 3 ones.
Input: ([0, 1, 0, 1, 0, 1], 1)
Expected Output: 1
Explanation: With k = 1 the answer is just whether any single position is on.
Hints
- A sliding window of fixed size k works directly; the only twist is wrapping. Compute the sum of the first k elements, then slide one position at a time.
- When you slide the window by one, add the element entering at index (i + k - 1) mod n and subtract the element leaving at index i - 1. This keeps each step O(1).
- You only need n window positions total (one starting at each index), so a single pass over n positions is sufficient — there is no need to physically duplicate the array.
Deletion Index Recovery
Constraints
- 0 <= len(s2) <= 10^5
- len(s1) == len(s2) + 1 in the valid case (otherwise return [])
- Strings consist of printable characters
- Indices in the result are 0-based and returned in increasing order
Examples
Input: ("abcde", "abde")
Expected Output: [2]
Explanation: Only removing index 2 ('c') yields 'abde'.
Input: ("aaa", "aa")
Expected Output: [0, 1, 2]
Explanation: All three characters are identical, so deleting any index yields 'aa'.
Input: ("abc", "ab")
Expected Output: [2]
Explanation: s2 is a prefix of s1; the deletion is the last character at index 2.
Input: ("a", "")
Expected Output: [0]
Explanation: Deleting the only character gives the empty string.
Input: ("aab", "ab")
Expected Output: [0, 1]
Explanation: Removing either of the two leading 'a's produces 'ab'.
Input: ("banana", "banaa")
Expected Output: [4]
Explanation: Only removing index 4 ('n') yields 'banaa'; the run of that character is length one.
Input: ("xyz", "yz")
Expected Output: [0]
Explanation: The first character differs immediately; deleting index 0 gives 'yz'.
Input: ("abb", "ab")
Expected Output: [1, 2]
Explanation: Removing either of the two trailing 'b's produces 'ab'.
Input: ("abc", "xy")
Expected Output: []
Explanation: s2 cannot be obtained from s1 by a single deletion, so the result is empty.
Hints
- Scan from the left to find the first index i where s1 and s2 differ. The deleted character must be s1[i]. If no mismatch is found, the deletion happened at the very end (i = len(s2)).
- After locating that first mismatch, verify the rest actually lines up: s1[i+1:] must equal s2[i:]. If it does not, s2 was not obtainable by a single deletion, so return an empty list.
- Repeated characters create multiple valid answers: any index inside the maximal run of the character s1[i] (extending both left and right from i) is also a valid deletion point. Expand left while s1[lo-1] == s1[i] and right while s1[hi+1] == s1[i], then return the full range [lo, hi].