## Amazon Interview Question for SDE-2s

Team: Product Details Page
Country: India
Interview Type: In-Person

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

``````public TreeNode FindCloset(TreeNode root, int value)
{
TreeNode current = root;
TreeNode minNode = null;
int minValue = int.MaxValue;

while (current != null)
{
int diff = Math.Abs(current.Value - value);
if (diff < minValue)
{
minValue = diff;
minNode = current;
if (diff == 0)
break;
}

current = (value < current.Value) ? current.Left : current.Right;
}

return minNode;
}``````

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

``````void findNearestNode(tree* root, int num, tree *outNode, int *out){
if (root == NULL) return;
if (*out > abs(root->data - num)){
*out = abs(root->data - num);
outNode->data = root->data;
}
if (num < root->data)
findNearestNode(root->left, num, outNode, out);
else
findNearestNode(root->right, num, outNode, out);
}

int main(){
tree nd;
int out = INT_MAX;
findNearestNode(root, 34, &nd, &out);
return 0;
}``````

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

``````Node closestNodeValuetoK( Node root, Node prev, int K){
//edge case
if( root == null)
return root;
if(root.data == K){
int upClosest = prev != null ? Math.abs(prev.data - K):Integer.MAXVALUE;
int leftClosest = root.left != null ? Math.abs(root.left.data - K):Integer.MAXVALUE;
int rightClosest = root.right != null ? Math.abs(root.right.data - K):Integer.MAXVALUE;
// closest node
int closest = Math.min( upClosest, Math.min( leftClosest, rightClosest ) );
// edge case
if( closest == Integer.MAXVALUE ) return null;
else{
if( closest = upClosest ) return prev;
else if ( closest == leftClosest ) return root.left;
else return root.right;
}
}
if ( K < root.data) // iterate left
return closestNodeValuetoK ( root.left, root, K);
else // iterate right
return closestNodeValuetoK ( root.right, root, K);
}``````

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

``````public int getClosestValue(Node n, int value) {
if (value > n.value) {
if (n.right == null) {
return n.value;
}
else {
int subClosest = getClosestValue(n.right, value);
if (Math.abs(subClosest - value) > Math.abs(n.value - value)) {
return n.value;
}
return subClosest;
}
}
else {
if (n.left == null) {
return n.value;
}
else {
int subClosest = getClosestValue(n.left, value);
if (Math.abs(subClosest - value) > Math.abs(n.value - value)) {
return n.value;
}
return subClosest;
}
}
}``````

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

``````struct bstnode* get_closest_value(struct bstnode* root, int k)
{
struct bstnode* node;
if (root == NULL) return root;

if (root->data == k) return root;
else if (root->data < k)
{
if (root->left == NULL) return root;
else
{

node = get_closest_value(root->left, k);
return(abs(node->data - k) > abs(root->data - k) ? root : node);
}
}
else
{
if (root->right == NULL) return root;
else
{
node = get_closest_value(root->right, k);
return(abs(node->data - k) > abs(root->data - k) ? root : node);
}
}
}``````

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

I am wondering that the K is Integer or float?

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

``````int closestToK(TreeNode * root, int k)
{
if (root -> val == k)
return root -> val;
int l, r, used_l = 0, used_r = 0;
if (root -> left){
used_l = 1;
l = closestToK(root -> left, k);
}
if (root -> right){
used_r = 1;
r = closestToK(root -> right, k);
}
int result = root -> val;
if (used_l && abs(result - k) > abs(l - k))
result = l;
if (used_r && abs(result - k) > abs(r - k))
result = r;
return result;
}
int closest_main(TreeNode * root, int k){
if (!root)
return 0;
else
return closestToK(root, k);
}``````

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

Use binary search algorithm and keep tracking the lower bound and upper bound as goes deep into the tree. If found in the tree it works just like binary search. Otherwise hitting the bottom of the tree, compare its distance with the lower bound and upper bound. Pick the bound with smaller distance. Details: cpluspluslearning-petert.blogspot.co.uk/2016/03/bst-find-closest-value.html

Two implementations: recursive and non-recursive.

Test

``````void TestFindCloesetNode_R()
{
TreeNode<double> *root = NULL;
assert(FindClosetNode_R(root, 0.0) == NULL);

std::vector<double> input = { 1.0 };
root = ConstructTreeRecursive(input);
assert(root);
TreeNode<double> *foundNode = FindClosetNode_R(root, 1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, 100.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, -100.0);
assert(*foundNode->data == 1.0);
DeleteTree_BU(&root);

input = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 };
root = ConstructTreeOnSortedValueBFS(input);
assert(root);
foundNode = FindClosetNode_R(root, -100.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, -1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, -1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, 1.2);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode_R(root, 1.6);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode_R(root, 2.0);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode_R(root, 2.1);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode_R(root, 2.6);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode_R(root, 3.0);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode_R(root, 3.4);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode_R(root, 3.6);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode_R(root, 4.0);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode_R(root, 4.4);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode_R(root, 4.6);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode_R(root, 5.0);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode_R(root, 5.3);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode_R(root, 5.6);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode_R(root, 6.0);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode_R(root, 6.3);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode_R(root, 6.6);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode_R(root, 7.0);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode_R(root, 7.3);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode_R(root, 7.6);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode_R(root, 8.0);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode_R(root, 8.3);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode_R(root, 8.6);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode_R(root, 9.0);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode_R(root, 9.3);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode_R(root, 9.6);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode_R(root, 10.0);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode_R(root, 10.3);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode_R(root, 10.6);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode_R(root, 11.0);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode_R(root, 11.3);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode_R(root, 11.6);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode_R(root, 12.0);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode_R(root, 12.3);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode_R(root, 12.6);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode_R(root, 13.0);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode_R(root, 13.3);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode_R(root, 13.6);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode_R(root, 14.0);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode_R(root, 14.3);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode_R(root, 14.6);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode_R(root, 15.0);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode_R(root, 15.3);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode_R(root, 15.6);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode_R(root, 16.0);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode_R(root, 16.3);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode_R(root, 100.0);
assert(*foundNode->data == 16.0);
DeleteTree_TD(&root);
}

void TestFindCloesetNode()
{
TreeNode<double> *root = NULL;
assert(FindClosetNode(root, 0.0) == NULL);

std::vector<double> input = { 1.0 };
root = ConstructTreeRecursive(input);
assert(root);
TreeNode<double> *foundNode = FindClosetNode(root, 1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, 100.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, -100.0);
assert(*foundNode->data == 1.0);
DeleteTree_BU(&root);

input = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 };
root = ConstructTreeOnSortedValueBFS(input);
assert(root);
foundNode = FindClosetNode(root, -100.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, -1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, -1.0);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, 1.2);
assert(*foundNode->data == 1.0);
foundNode = FindClosetNode(root, 1.6);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode(root, 2.0);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode(root, 2.1);
assert(*foundNode->data == 2.0);
foundNode = FindClosetNode(root, 2.6);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode(root, 3.0);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode(root, 3.4);
assert(*foundNode->data == 3.0);
foundNode = FindClosetNode(root, 3.6);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode(root, 4.0);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode(root, 4.4);
assert(*foundNode->data == 4.0);
foundNode = FindClosetNode(root, 4.6);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode(root, 5.0);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode(root, 5.3);
assert(*foundNode->data == 5.0);
foundNode = FindClosetNode(root, 5.6);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode(root, 6.0);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode(root, 6.3);
assert(*foundNode->data == 6.0);
foundNode = FindClosetNode(root, 6.6);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode(root, 7.0);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode(root, 7.3);
assert(*foundNode->data == 7.0);
foundNode = FindClosetNode(root, 7.6);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode(root, 8.0);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode(root, 8.3);
assert(*foundNode->data == 8.0);
foundNode = FindClosetNode(root, 8.6);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode(root, 9.0);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode(root, 9.3);
assert(*foundNode->data == 9.0);
foundNode = FindClosetNode(root, 9.6);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode(root, 10.0);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode(root, 10.3);
assert(*foundNode->data == 10.0);
foundNode = FindClosetNode(root, 10.6);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode(root, 11.0);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode(root, 11.3);
assert(*foundNode->data == 11.0);
foundNode = FindClosetNode(root, 11.6);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode(root, 12.0);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode(root, 12.3);
assert(*foundNode->data == 12.0);
foundNode = FindClosetNode(root, 12.6);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode(root, 13.0);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode(root, 13.3);
assert(*foundNode->data == 13.0);
foundNode = FindClosetNode(root, 13.6);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode(root, 14.0);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode_R(root, 14.3);
assert(*foundNode->data == 14.0);
foundNode = FindClosetNode(root, 14.6);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode(root, 15.0);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode(root, 15.3);
assert(*foundNode->data == 15.0);
foundNode = FindClosetNode(root, 15.6);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode(root, 16.0);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode(root, 16.3);
assert(*foundNode->data == 16.0);
foundNode = FindClosetNode(root, 100.0);
assert(*foundNode->data == 16.0);
DeleteTree_TD(&root);
}``````

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

``````public int bstClosestValue(int k) {
int closest = _bstClosestValue(bst, k);
System.out.println("Closes : " + closest);
return closest;
}

private int _bstClosestValue(TreeNode node, int k) {
int closest = node.data;
int prediff = Math.abs(node.data - k);
Stack<TreeNode> stack = new Stack<TreeNode>();
while (!stack.isEmpty() || node != null) {
if (node != null) {
stack.push(node);
node = node.left;
} else {
node = stack.pop();
int currdiff = Math.abs(node.data - k);
if (currdiff < prediff) {
closest = node.data;
prediff = currdiff;
}
node = node.right;
}
}
return closest;
}``````

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.