Interview Question

Machine Learning Scientist Interview



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

AnswerAdd Tags

Interview Answers

2 Answers


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.

Argho on


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)

Aaron on

Add Answers or Comments

To comment on this, Sign In or Sign Up.