0 like 0 dislike
1,386 views

All interview experiences for Google  can be found using the tag "google_interview_experiences" in the search bar.

| 1,386 views

0 like 0 dislike

Problem Statement:
You are given a distance d and a stream of float numbers (i.e., numbers are given one at a time). Your task is to write a method that takes this number as an input, and checks for a group of three numbers, where each number is within a distance 'd' of other two.
If found, remove them from your collection and return.

Lets say for a group of 3 numbers found, a, b and c, the following condition holds:

``````|a - b| <= d and |b - c| <= d and |c - a| <= d
``````

Example:
for a distance d = 1,
input = 1.1, action => None
input = 2.1, action => None
input = 3.1, action => None
input = 1.5, action => Remove and return {1.1, 2.1, 1.5} as an output.

Clarifying questions that I remember asking:

• Is d inclusive for distance between two number? -> Yes
• What if the new number received leads to formation of multiple such triplets? -> We decided that smallest three numbers we would remove.
• Any desired order of output values? -> No.

by Expert (35,210 points)