Maximum Length of Repeated Subarray
Problem Statement
You get two arrays of integers, nums1 and nums2. Return the length of the longest subarray that appears in both arrays.
A subarray is a run of values that sit next to each other. A gap is not allowed.
Two integer arrays nums1 and nums2.
The length of the longest subarray that appears in both arrays.
The repeated subarray is [3, 2, 1]. It sits at positions 2–4 in nums1 and at positions 0–2 in nums2.
All five zeros match, so the whole array is the repeated subarray.
- • 1 ≤ nums1.length, nums2.length ≤ 1000
- • 0 ≤ nums1[i], nums2[i] ≤ 100
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every pair of start positions and extend the run while the values match
Fill an (N + 1) × (M + 1) table; a match extends the diagonal cell by one
Keep one row; on a match row[j] = row[j − 1] + 1, on a mismatch 0; read j from high to low
Pick a start in nums1 and a start in nums2. Walk both forward together while the values match, and record how far they go. Do this for every pair of starts and keep the longest run. Each walk starts from scratch, so the same values are compared again and again.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.