Google Onsite round question for L3 role

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-12-01Added By Anonymous
Source: Link

Company: Google
Role: L3
Interview Stage: Onsite
Location: unknown
Year: unknown

Original Post Content:

In the third Google onsite round, I was asked the question similar to the one below related to arrays:

Given are Three Arrays: A, B, C
- Non-negative integers
- SORTED ARRAYS
- have the same length N

A non-negative integer D.

Find the number of triplets (i, j, k) such that, they follow these constraints:
|A[i] - B[j]| <= D
|A[i] - C[k]| <= D
|B[j] - C[k]| <= D

This is equivalent to: |max() - min()| <= D for the triplet (A[i], B[j], C[k])

Example:
Input: A = [1, 5, 5], B = [3, 4, 5], C = [6, 6, 8], D = 1
Output: 2
Explanation: The triplets (5,5,6) from indices (2,2,0) and (2,2,1) give 2 triplets.

Solve the question in O(n log n) time complexity and O(1) space complexity.

Comments