Reported July 2026
DE Shawdynamic programming

Maximum L1 Distance Between Equal-Length Subarrays

Reported by candidates from DE Shaw's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live DE Shaw OA. Under 2s to a working solution.
Founder's read

The DE Shaw OA reported in July 2026 hides a nasty detail in its statement: the two subarrays must match in length but can start at different indices. That kills any single-pass trick and points straight at diagonal DP. With both arrays capped at 2000, an O(n*m) solution is the target. If you blank on how to handle the absolute values, StealthCoder runs invisibly during the live OA as a safety net. Here's the shape of the answer before you open the timer.

The problem

Given two integer arrays a and b, choose one nonempty contiguous subarray from each array. The two chosen subarrays must have the same length, but they may start at different indices.
If the chosen subarrays start at indices i and j and have length length, their L1 distance is
|a[i] - b[j]| + |a[i + 1] - b[j + 1]| +... + |a[i + length - 1] - b[j + length - 1]|
Return the maximum possible L1 distance. A subarray of length 1 is valid.

Function
maxL1Distance(a: int[], b: int[]) → long

Examples
Example 1
a = [1,3,2]
b = [4,2]
return = 4
Choose [1,3] from a and [4,2] from b. Their distance is |1 - 4| + |3 - 2| = 3 + 1 = 4.
Example 2
a = [100,0]
b = [100,-100]
return = 200
Choose the length-1 subarrays [100] from the start of a and [-100] from the end of b. Their distance is 200.
Example 3
a = [-3,4]
b = [1,-2,5]
return = 10
Align [-3,4] with [1,-2]. The distance is |-3 - 1| + |4 - (-2)| = 4 + 6 = 10, which is maximal.

Constraints
1 <= a.length, b.length <= 2000
-10^9 <= a[i], b[i] <= 10^9
The answer fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: fix the offset d = j - i. Every pair of aligned subarrays lives on one diagonal of the n by m grid, where cell (i,j) holds |a[i] - b[j]|. Since all values are nonnegative, the maximum over subarrays on a diagonal is just the whole diagonal from that cell to its end, but the diagonal sum is already the best, so you simply sum each full diagonal and take the max. Equivalently, run a Kadane-style DP: dp[i][j] = |a[i]-b[j]| + max(0, dp[i-1][j-1]). Because every term is nonnegative, the max(0,...) never hurts. The common pitfall is using int instead of long. Sums reach 2000 * 2*10^9, which overflows 32 bits. Another pitfall is trying to brute force all lengths and starts, which is O(n*m*min) and times out. If you freeze on the recurrence during the live OA, StealthCoder is the hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Maximum L1 Distance Between Equal-Length Subarrays cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass DE Shaw's OA.

DE Shaw reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum L1 Distance Between Equal-Length Subarrays FAQ

What's the trick in the DE Shaw maximum L1 distance problem?+

Treat each starting offset as a diagonal of the grid where cell (i,j) is |a[i]-b[j]|. Aligned subarrays are segments of a diagonal. Since every term is nonnegative, the longest segment wins, so you sum full diagonals and take the max. It runs in O(n*m).

How hard is this problem really?+

Medium at most. The statement looks intimidating because of the two free start indices, but once you see diagonals it's a few lines. The real risk is overflow, not the algorithm. Use 64-bit integers for every sum.

Do I need a Kadane-style DP or is a plain diagonal sum enough?+

Plain diagonal sum is enough, because absolute differences are never negative, so extending a subarray never lowers the total. A Kadane DP with max(0, previous) gives the same answer and is a fine fallback if you prefer a recurrence.

What complexity should I aim for given the constraints?+

Both arrays go up to 2000, so O(n*m) is about 4 million operations, which is comfortable. Anything cubic, like trying every length and both start indices, will time out. Space can be O(1) if you walk each diagonal directly.

How do I prepare for this in 48 hours?+

Write the diagonal solution once from scratch and test it against the three examples, especially example 2 where length 1 wins. Then check edge cases: single-element arrays, arrays of different lengths, and values at plus or minus 10^9 to confirm no overflow.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with DE Shaw.

OA at DE Shaw?
Invisible during screen share
Get it