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.
nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Example 1:
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Example 2:
Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
Constraints:
1 <= nums.length <= 10001 <= sum(nums[i].length) <= 10001 <= nums[i][j] <= 1000nums[i] are unique.Problem summary: Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
[[1,2,3],[4,5,6]]
intersection-of-two-arrays)intersection-of-two-arrays-ii)find-smallest-common-element-in-all-rows)intersection-of-three-sorted-arrays)find-the-difference-of-two-arrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2248: Intersection of Multiple Arrays
class Solution {
public List<Integer> intersection(int[][] nums) {
int[] cnt = new int[1001];
for (var arr : nums) {
for (int x : arr) {
++cnt[x];
}
}
List<Integer> ans = new ArrayList<>();
for (int x = 0; x < 1001; ++x) {
if (cnt[x] == nums.length) {
ans.add(x);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2248: Intersection of Multiple Arrays
func intersection(nums [][]int) (ans []int) {
cnt := [1001]int{}
for _, arr := range nums {
for _, x := range arr {
cnt[x]++
}
}
for x, v := range cnt {
if v == len(nums) {
ans = append(ans, x)
}
}
return
}
# Accepted solution for LeetCode #2248: Intersection of Multiple Arrays
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
cnt = [0] * 1001
for arr in nums:
for x in arr:
cnt[x] += 1
return [x for x, v in enumerate(cnt) if v == len(nums)]
// Accepted solution for LeetCode #2248: Intersection of Multiple Arrays
/**
* [2248] Intersection of Multiple Arrays
*
* Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
*
* Example 1:
*
* Input: nums = [[<u>3</u>,1,2,<u>4</u>,5],[1,2,<u>3</u>,<u>4</u>],[<u>3</u>,<u>4</u>,5,6]]
* Output: [3,4]
* Explanation:
* The only integers present in each of nums[0] = [<u>3</u>,1,2,<u>4</u>,5], nums[1] = [1,2,<u>3</u>,<u>4</u>], and nums[2] = [<u>3</u>,<u>4</u>,5,6] are 3 and 4, so we return [3,4].
* Example 2:
*
* Input: nums = [[1,2,3],[4,5,6]]
* Output: []
* Explanation:
* There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
*
*
* Constraints:
*
* 1 <= nums.length <= 1000
* 1 <= sum(nums[i].length) <= 1000
* 1 <= nums[i][j] <= 1000
* All the values of nums[i] are unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/intersection-of-multiple-arrays/
// discuss: https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn intersection(nums: Vec<Vec<i32>>) -> Vec<i32> {
let target_count = nums.len();
nums.into_iter()
.flatten()
.fold(std::collections::HashMap::new(), |mut counts, n| {
*counts.entry(n).or_insert(0_usize) += 1;
counts
})
.into_iter()
.filter_map(|(n, count)| (count == target_count).then_some(n))
.collect::<std::collections::BinaryHeap<_>>()
.into_sorted_vec()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2248_example_1() {
let nums = vec![vec![3, 1, 2, 4, 5], vec![1, 2, 3, 4], vec![3, 4, 5, 6]];
let result = vec![3, 4];
assert_eq!(Solution::intersection(nums), result);
}
#[test]
fn test_2248_example_2() {
let nums = vec![vec![1, 2, 3], vec![4, 5, 6]];
let result: Vec<i32> = vec![];
assert_eq!(Solution::intersection(nums), result);
}
}
// Accepted solution for LeetCode #2248: Intersection of Multiple Arrays
function intersection(nums: number[][]): number[] {
const cnt = new Array(1001).fill(0);
for (const arr of nums) {
for (const x of arr) {
cnt[x]++;
}
}
const ans: number[] = [];
for (let x = 0; x < 1001; x++) {
if (cnt[x] === nums.length) {
ans.push(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.