Google | Onsite | Find Group of 3 Numbers Within a Distance 'd'
Anonymous User
1616

Hi,

The following question was asked to me during an onsite round of google.

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.

Thanks.

Comments (8)