Amazon interview question

Given a number, how to find a closest number in a series of floating point data.

Interview Answers

Anonymous

12 Jun 2017

Use a Binary Tree data structure. It is sorted while it stores the elements. Then traverse the tree. Search is log N. In Java there is a ceiling and floor function which does a BFS traversal and tries to search the closest number rounded off, to the nearest below or above number on the basis of ceiling or Floor function.

Anonymous

17 Jul 2019

Just loop over the series. Calculate abs(ref_num-x_i). Remember index of last seen lowest abs difference. In the end just return the element at the index you have saved to the end. \theat(n) time \theta(1) space additionaly (besides space for series)