Count Quadruples From Four Shard Deltas That Cancel Out

Last updated: November 29, 2026

Quick Overview

Given four equal-length lists of integer counter deltas from four shards, count the tuples (i, j, k, l) such that A[i] + B[j] + C[k] + D[l] == 0.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given four equal-length lists of integer counter deltas from four shards, count the tuples (i, j, k, l) such that A[i] + B[j] + C[k] + D[l] == 0.

A meet-in-the-middle hashing question in the style of Google technical screens, framed as reconciling counter deltas across four replicas. The interviewer wants you to drop from O(n^4) to O(n^2) by hashing pair sums and to reason about the memory cost of that map.

What the Interviewer Expects
  • Identifies the brute force as O(n^4)
  • Splits into two halves and hashes all A+B sums
  • Counts matching -(C+D) sums with multiplicity
  • States O(n^2) time and space
  • Discusses reducing memory or handling very skewed sums
Key Topics to Cover
Meet in the Middle
Hash Map
Counting
Arrays
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if there are k lists; how does meet-in-the-middle generalize?
  • The lists are too large for an O(n^2) map in memory; what would you do?
  • Return the number of distinct value quadruples instead of index tuples.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: four lists A, B, C, D, each of length n (n up to about 200 to 500).
  • Output: number of index tuples with zero total; duplicates in the lists count separately....
High-Level Design

Checking every tuple costs n^4. Split the sum into (A[i] + B[j]) and (C[k] + D[l]). Build a Counter of all n^2 sums of A and B. Then for every pair from C and D, look up how many AB sums equal `-(...


Submit Your Answer
Markdown supported