## Amazon Interview Question for Interns

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````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!)
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

O(Log(N))

Search from Root to Leaf. if((fabs)(cur_node_left_val-input)>=(fabs)(cur_node_right_val-input)) then go to Left node Else got to Right node.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Time complexity : O(Log(N))

Search from Root to Leaf. if((fabs)(cur_node_left_val-input)>=(fabs)(cur_node_right_val-input)) then go to Left node Else got to Right node.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````//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;
}

}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
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) {
}
else if (diff1 < diff2) {
}
else {
}
return Math.min(diff1, diff2);
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.