Google Phone Screen

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-10-07Added By Anonymous
Source: Link

Company: Google
Role: unknown
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Given 3 sorted arrays (A, B, C) each of size n; and an integer D; you have to find number of tuples (i,j,k) such that:
|A[i]-B[j]|<=D
|B[j]-C[k]|<=D
|A[i]-C[k]|<=D

Provided time complexity of N2logN

But interviewer told that O(N), O(NlogN) solutions were also possible

PS : duplicates are possible

Edit : Yes, I could figure out the solution after the interview: either put all the points in a common array and sort them, then for each element find the number of triplets possible in range [element, element+d] -> O(N) or find all triplets such that A[i]<=B[j] and A[i]<C[k], B[j]<A[i] && B[j]<C[k], C[k]<=A[i]&& C[k]<B[j]. Finally check for triplets where A[i]=B[j]=C[k]. This could be done via sliding window O(N).

Unfortunately, I could not figure this out in the interview. Maybe I was too nervous :-(

Comments