Snowflake Coding Interview / OA: Find Number of Pairs and Tuples Within a Threshold

44 Views
No Comments

You are given two sorted arrays A, B and another integer D. Find the number of pairs (i, j) such that |A[i] - B[j]| <= D.

Follow up: You are given three sorted arrays A, B, C, and another integer D. Find the number of tuples (i, j, k) such that |A[i] - B[j]| <= D, |A[i] - C[k]| <= D, and |B[j] - C[k]| <= D.

This problem is a classic sorted-array counting task. For the first part, the key idea is to count pairs from A and B whose absolute difference is at most D, using two pointers or a sliding window thanks to the sorted order. The follow-up extends the same idea to three sorted arrays, where the goal is to count tuples that satisfy pairwise distance constraints within D. A good solution typically relies on maintaining valid ranges with pointers or binary search rather than brute force enumeration.

END
 0