## Amazon Interview Question

Interns**Country:**United States

**Interview Type:**Phone Interview

it's an easy question but not that simple:

- is the input an element in the tree or an arbitrary value, I assume an arbitrary value

- I assume the tree is balanced, so the time complexity of below code is O(lg(n)), otherwise O(n) is best. space complexity is here the same as time complexity unless you do the search by patching pointers temporarily.

The naive algo is inorder traversal, keeping the previous element and when the search value is passed, exploring the next element to see which value is closer. O(n)

More sophisticated is:

```
const Node* nearestElement(const Node* root, int value, const Node* nearest = nullptr)
{
if(root == null) return nearest;
if(root->value_ == value) return root;
if(nearest == nullptr || abs(root->value_ - value) < abs(nearest->value_ - value)) {
nearest = root;
}
if(root->value_ > value) {
return nearestElement(root->left_, value, nearest);
}
return nearestElement(root->right_, value, nearest);
}
```

Following java method returns a node with same or nearest value.

```
public node findNearestNode(int val, node root){
if(root == null) return null;
int dis = Integer.MAX_VALUE,i = 0;
node nearestNode = root;
node temp = root;
while(temp != null){
if(val == temp.value){
nearestNode = temp;
break;
}
i = Math.abs(temp.value - val);
if(dis > i){
dis = i;
nearestNode = temp;
}
if(val > temp.value){
temp = temp.rightNode;
}else{
temp = temp.leftNode;
}
}
return nearestNode;
}
```

```
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var root = new Node(7);
root.Right = new Node(15);
root.Left = new Node(3);
root.Right.Right = new Node(20);
root.Right.Left = new Node(10);
root.Left.Right = new Node(5);
root.Left.Left = new Node(1);
var result = GetNearestElement(root, 4, root, int.MaxValue);
if (result != null)
{
Console.WriteLine("Element Found -> {0}", result.Value);
}
else
{
Console.WriteLine("No Element was Found ");
}
}
public static Node GetNearestElement(Node root, int val, Node nearestNode, int diff)
{
if (root.Value == val)
return root;
if (Math.Abs(root.Value - val) < diff)
{
nearestNode = root;
diff = Math.Abs(root.Value - val);
}
if (root.Value > val && root.Left != null)
return GetNearestElement(root.Left, val, nearestNode, diff);
else if (root.Value < val && root.Right != null)
return GetNearestElement(root.Right, val, nearestNode, diff);
else
{
return nearestNode;
}
}
}
public class Node
{
public int Value{ get; set;}
public Node Right{ get; set;}
public Node Left{ get; set;}
public Node(int val)
{
this.Value = val;
}
}
}
```

The solution is to customize the original BST search by:

Finding the diff when traversing the tree, if the new diff is less than the current min diff, update the return elements and min diff = new diff.

Below is the code snippet:

```
public Set<String> findNearest(Tree<Integer> tree, int num) {
int minDiff = Integer.MAX_VALUE;
Set<String> retVals = new HashSet<String>();
Tree.Node<Integer> curNode = tree.getRoot();
while (curNode != null) {
if (curNode.getVal() == num) {
retVals.clear();
retVals.add(curNode.id());
break;
}
else {
Tree.Node<Integer> nextNode = curNode.getVal() > num ? curNode.left() : curNode.right();
Set<String> curVals = new HashSet<String>();
if (nextNode != null) {
int curDiff = findNearestSub(curNode, nextNode, num, curVals);
if (curDiff <= minDiff) {
minDiff = curDiff;
retVals = curVals;
}
}
curNode = nextNode;
}
}
return retVals;
}
public int findNearestSub(Tree.Node<Integer> n1, Tree.Node<Integer> n2, int num, Set<String> retVals) {
int diff1 = Math.abs(n1.getVal() - num);
int diff2 = Math.abs(n2.getVal() - num);
if (diff1 > diff2) {
retVals.add(n2.id());
}
else if (diff1 < diff2) {
retVals.add(n1.id());
}
else {
retVals.add(n2.id());
retVals.add(n1.id());
}
return Math.min(diff1, diff2);
}
```

- Anonymous December 26, 2017