Longest Common Contiguous Subarray
Updated
Problem statement
Given arr1 and arr2, return the longest sequence of consecutive values that appears in both arrays. The values must appear in the same order, without skipping any elements.
For this exercise, assume the inputs contain integers and return the selected values, rather than only the length. If several longest occurrences exist, choose the smallest starting index in arr1, then the smallest starting index in arr2.
For this exercise, assume an empty array is returned when no nonempty common subarray exists, including when either input is empty. Preserve signed values and repeated elements.
Function
longestCommonSubarray(arr1: int[], arr2: int[]) → int[]Examples
Example 1
arr1 = [1,2,3,2,1]arr2 = [3,2,1,4,7]return = [3,2,1]This example returns the three-element contiguous run, rather than a subsequence or only its length.
Example 2
arr1 = [4,5,9,1,2]arr2 = [1,2,8,4,5]return = [4,5]This tie chooses [4,5], whose occurrence starts earlier in the first array.
Example 3
arr1 = [-1,0,-1,2]arr2 = [8,-1,0,-1,3]return = [-1,0,-1]This example preserves negative values, zero and repeated values in the returned run.
Constraints
- For this exercise, assume
0 <= arr1.length, arr2.length <= 500. - For this exercise, assume every element is an integer from
-1000through1000.
More Coinbase interview questions
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · Seen Apr 2022
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Feb 2022
- Justify Text to a Fixed WidthPHONE SCREEN · Seen Jun 2021
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Sliding-Window Averages as Reduced FractionsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Maximum Subarray Sum with One DeletionServiceNow · PHONE SCREENServiceNow interview questions
- Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- House RobberVisa · PHONE SCREENVisa interview questions
- Best Improvement Period of at Least K IntervalsAmazon · PHONE SCREEN
- Longest Increasing SubsequenceAngel One · PHONE SCREEN
- Match Shops and BuyersGoogle · PHONE SCREENGoogle interview questions