Maximum L1 Distance Between Equal-Length Subarrays
Company: Deshaw
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
Implement `max_l1_subarray_distance(a, b)` for two nonempty integer arrays. Choose one nonempty contiguous subarray from `a` and one nonempty contiguous subarray from `b`. The two chosen subarrays must have the same length. Their L1 distance is the sum of the absolute differences between corresponding elements. Return the maximum possible distance over every valid pair of subarrays.
## Constraints
- `1 <= len(a), len(b) <= 2,000`
- `-10^9 <= a[i], b[i] <= 10^9`
- Length-one subarrays are valid.
- Use integer arithmetic; the answer may exceed 32-bit range.
## Clarifications
The two subarrays may start at different indices. Their common length may be any value from one through `min(len(a), len(b))`. Return only the maximum distance, not the subarray indices.
## Examples
`a = [1, 3]`, `b = [2, 0]` returns `4`, obtained from both full arrays: `|1-2| + |3-0| = 4`.
`a = [5]`, `b = [1, 9]` returns `4`.
## Hint
Pairs of elements with a fixed difference between their indices lie on the same alignment diagonal. Consider what the best nonempty contiguous range on each such diagonal looks like when every contribution is nonnegative.
## Interview Follow-ups
- Return the two start indices and the chosen length as well as the distance.
- Explain the time and auxiliary-space complexity.
- Discuss what changes if each aligned pair may contribute a negative score instead of an absolute difference.
Quick Answer: Find the maximum L1 distance between equal-length nonempty contiguous subarrays chosen from two integer arrays. Handle different start positions, any valid common length, large values, 64-bit results, optional witness indices, and the required time and space analysis.
Implement `max_l1_subarray_distance(a, b)` for two nonempty integer arrays. Select one nonempty contiguous subarray from `a` and one nonempty contiguous subarray from `b`. The selected subarrays must have the same length. Their L1 distance is the sum of the absolute differences between corresponding elements. Return the maximum distance over every valid pair. The two subarrays may start at different indices, and their common length may range from 1 through `min(len(a), len(b))`. Return only the maximum distance, not the indices.
Constraints
- 1 <= len(a), len(b) <= 2,000
- -10^9 <= a[i], b[i] <= 10^9
- The selected subarrays are nonempty and have equal length.
- The selected subarrays may start at different indices.
- The common length is between 1 and min(len(a), len(b)), inclusive.
- Use integer arithmetic; the result may exceed 32-bit range and is at most 4,000,000,000,000.
Examples
Input: ([0], [0])
Expected Output: 0
Explanation: The sole legal subarray pair contains equal elements, so its L1 distance is zero.
Input: ([1, 3], [2, 0])
Expected Output: 4
Explanation: The full alignment contributes |1 - 2| + |3 - 0| = 1 + 3 = 4.
Hints
- Pairs whose indices have a fixed difference lie on one alignment diagonal.
- On one diagonal, determine whether excluding a nonnegative absolute-difference contribution can improve the sum.