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.
Move from brute-force thinking to an efficient approach using array strategy.
Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:
0 <= i, j, k, l < nnums1[i] + nums2[j] + nums3[k] + nums4[l] == 0Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] Output: 1
Constraints:
n == nums1.lengthn == nums2.lengthn == nums3.lengthn == nums4.length1 <= n <= 200-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228Problem summary: Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: 0 <= i, j, k, l < n nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2] [-2,-1] [-1,2] [0,2]
[0] [0] [0] [0]
4sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #454: 4Sum II
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int a : nums1) {
for (int b : nums2) {
cnt.merge(a + b, 1, Integer::sum);
}
}
int ans = 0;
for (int c : nums3) {
for (int d : nums4) {
ans += cnt.getOrDefault(-(c + d), 0);
}
}
return ans;
}
}
// Accepted solution for LeetCode #454: 4Sum II
func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int) {
cnt := map[int]int{}
for _, a := range nums1 {
for _, b := range nums2 {
cnt[a+b]++
}
}
for _, c := range nums3 {
for _, d := range nums4 {
ans += cnt[-(c + d)]
}
}
return
}
# Accepted solution for LeetCode #454: 4Sum II
class Solution:
def fourSumCount(
self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]
) -> int:
cnt = Counter(a + b for a in nums1 for b in nums2)
return sum(cnt[-(c + d)] for c in nums3 for d in nums4)
// Accepted solution for LeetCode #454: 4Sum II
use std::collections::HashMap;
impl Solution {
pub fn four_sum_count(
nums1: Vec<i32>,
nums2: Vec<i32>,
nums3: Vec<i32>,
nums4: Vec<i32>,
) -> i32 {
let mut cnt = HashMap::new();
for &a in &nums1 {
for &b in &nums2 {
*cnt.entry(a + b).or_insert(0) += 1;
}
}
let mut ans = 0;
for &c in &nums3 {
for &d in &nums4 {
if let Some(&count) = cnt.get(&(0 - (c + d))) {
ans += count;
}
}
}
ans
}
}
// Accepted solution for LeetCode #454: 4Sum II
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
const cnt: Record<number, number> = {};
for (const a of nums1) {
for (const b of nums2) {
const x = a + b;
cnt[x] = (cnt[x] || 0) + 1;
}
}
let ans = 0;
for (const c of nums3) {
for (const d of nums4) {
const x = c + d;
ans += cnt[-x] || 0;
}
}
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.