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.
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'.
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].