Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values:
answer1 : the number of indices i such that nums1[i] exists in nums2.answer2 : the number of indices i such that nums2[i] exists in nums1.Return [answer1,answer2].
Example 1:
Input: nums1 = [2,3,2], nums2 = [1,2]
Output: [2,1]
Explanation:
Example 2:
Input: nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
Output: [3,4]
Explanation:
The elements at indices 1, 2, and 3 in nums1 exist in nums2 as well. So answer1 is 3.
The elements at indices 0, 1, 3, and 4 in nums2 exist in nums1. So answer2 is 4.
Example 3:
Input: nums1 = [3,4,2,3], nums2 = [1,5]
Output: [0,0]
Explanation:
No numbers are common between nums1 and nums2, so answer is [0,0].
Constraints:
n == nums1.lengthm == nums2.length1 <= n, m <= 1001 <= nums1[i], nums2[i] <= 100Problem summary: You are given two integer arrays nums1 and nums2 of sizes n and m, respectively. Calculate the following values: answer1 : the number of indices i such that nums1[i] exists in nums2. answer2 : the number of indices i such that nums2[i] exists in nums1. Return [answer1,answer2].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[2,3,2] [1,2]
[4,3,2,3,1] [2,2,5,2,3,6]
[3,4,2,3] [1,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
class Solution {
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
int[] s1 = new int[101];
int[] s2 = new int[101];
for (int x : nums1) {
s1[x] = 1;
}
for (int x : nums2) {
s2[x] = 1;
}
int[] ans = new int[2];
for (int x : nums1) {
ans[0] += s2[x];
}
for (int x : nums2) {
ans[1] += s1[x];
}
return ans;
}
}
// Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
func findIntersectionValues(nums1 []int, nums2 []int) []int {
s1 := [101]int{}
s2 := [101]int{}
for _, x := range nums1 {
s1[x] = 1
}
for _, x := range nums2 {
s2[x] = 1
}
ans := make([]int, 2)
for _, x := range nums1 {
ans[0] += s2[x]
}
for _, x := range nums2 {
ans[1] += s1[x]
}
return ans
}
# Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
class Solution:
def findIntersectionValues(self, nums1: List[int], nums2: List[int]) -> List[int]:
s1, s2 = set(nums1), set(nums2)
return [sum(x in s2 for x in nums1), sum(x in s1 for x in nums2)]
// Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
// class Solution {
// public int[] findIntersectionValues(int[] nums1, int[] nums2) {
// int[] s1 = new int[101];
// int[] s2 = new int[101];
// for (int x : nums1) {
// s1[x] = 1;
// }
// for (int x : nums2) {
// s2[x] = 1;
// }
// int[] ans = new int[2];
// for (int x : nums1) {
// ans[0] += s2[x];
// }
// for (int x : nums2) {
// ans[1] += s1[x];
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2956: Find Common Elements Between Two Arrays
function findIntersectionValues(nums1: number[], nums2: number[]): number[] {
const s1: number[] = Array(101).fill(0);
const s2: number[] = Array(101).fill(0);
for (const x of nums1) {
s1[x] = 1;
}
for (const x of nums2) {
s2[x] = 1;
}
const ans: number[] = Array(2).fill(0);
for (const x of nums1) {
ans[0] += s2[x];
}
for (const x of nums2) {
ans[1] += s1[x];
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.