Amazon Interview Question
InternsCountry: United States
Interview Type: Phone Interview
class Node : NSObject
{
init(left : Node?,right : Node?,value : Int)
{
super.init()
self.left = left
self.right = right
self.value = value
}
var left : Node?
var right : Node?
var value : Int?
}
func findNearestValuFromTree(node : Node?,value : Int) -> Int?
{
if(node == nil)
{
return nil
}
return find(node: node, value: value, distance: abs(node!.value! - value), minValue: node!.value!)
}
func find(node : Node?,value : Int,distance : Int, minValue : Int) -> Int
{
if node == nil
{
return minValue
}
if node!.value! == value {
return value
}
print("n:\(node!.value!) v\(value) d:\(distance)")
print("newD:\(abs(node!.value! - value)) d:\(distance)\n")
if abs(node!.value! - value) > distance
{
return minValue
}
if value < node!.value!
{
return find(node: node?.left, value: value, distance: abs(node!.value! - value), minValue: node!.value!)
}
return find(node: node?.right , value: value, distance: abs(node!.value! - value), minValue: node!.value!)
}
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;
}
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);
}
- abdelrahman.elbarbary January 03, 2018