MediumArraysAI interview only
Maximum Length of Repeated Subarray
Asked atgoogleamazonmicrosoftbloomberg
01 · Problem
Given two integer arrays nums1 and nums2, return the length of the longest contiguous subarray that appears in both arrays (same values in the same order). If the arrays share no common value, return 0.
02 · Examples
Example 01
Input
nums1 = [1,2,3,2,1], nums2 = [3,2,1,4,7]
Output
3
The subarray [3,2,1] appears in both arrays, and no longer common subarray exists.
Example 02
Input
nums1 = [0,0,0,0,0], nums2 = [0,0,0,0,0]
Output
5
The two arrays are identical, so the whole array of length 5 is shared.
Example 03
Input
nums1 = [1,2,3], nums2 = [4,5,6]
Output
0
The arrays have no value in common, so the answer is 0.
03 · Constraints
- 011 <= nums1.length, nums2.length <= 1000
- 020 <= nums1[i], nums2[i] <= 100
- 03A subarray must be contiguous and non-empty; return 0 if the arrays share no element
04 · Optimal complexity
- Time
- O(m * n)
- Space
- O(min(m, n))
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.