Count Quadruples Across Four Signal Arrays That Cancel Out
Last updated: November 29, 2026
Quick Overview
Given four equal-length integer arrays A, B, C and D of ranking signal adjustments, count the index tuples (i, j, k, l) with A[i] + B[j] + C[k] + D[l] equal to zero.
Meta
November 29, 20260
0
0 solved
Given four equal-length integer arrays A, B, C and D of ranking signal adjustments, count the index tuples (i, j, k, l) with A[i] + B[j] + C[k] + D[l] equal to zero.
A meet-in-the-middle hashing problem. At Meta it tests whether you can split a four-way search into two pairwise halves and trade memory for time, a pattern that shows up again in feature-crossing and dedupe work.
What the Interviewer Expects
- Identifies O(n^4) brute force and the O(n^2) meet-in-the-middle idea
- Counts all A + B sums in a hash map
- Looks up the negated C + D sums and accumulates counts
- Discusses the O(n^2) memory cost and how to reduce it
- Handles duplicates by counting, not by storing pairs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Memory is limited to O(n). What can you do?
- Generalize to k arrays and a target T.
- Return one valid quadruple, not the count.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: four lists of length n (n up to about 500) with integer values. Output: the number of tuples summing to 0. Tuples are by index, so repeated values contribute repeatedly....
High-Level Design
Split the four arrays into two pairs. Build ab[x] = number of (i, j) with A[i] + B[j] = x. Then for every (k, l), the tuple completes when A[i] + B[j] = -(C[k] + D[l]), so add `ab[-(C[k] + D[l])...