Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

void findNext(Node *root,int target){
    public static Node * prev = NULL;
    if(root == NULL)
        return;
    else{
        findNext(root->left,target);
        if(prev != NULL){
            if(prev -> value == target && prev->value < root->value){
                printf("%d",root->value);
        prev = root;    
        findNext(root->right,target);
    }
}

- Coder January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tree* searchLeftmostnodeInRightSubtree (tree* root)
{
        while (root->left)
        {
                root = root->left;
        }
        return root;
}

tree* inorderSuccessor (tree *root, int key)
{
        if (root)
        {
                if (root->key == key)
                {
                        /*inorder successor will be in right subtree of node if right subtree exist*/
                        if (root->right)
                        {
                                tree *subtree = searchLeftmostnodeInRightSubtree(root->right);
                                return subtree;
                        }
                        /*If right subtree of this node doesnot exist, then parent node for which leftsubtree consist this node*/
                        else
                        {
                                return root;
                        }
                }
                tree *left = inorderSuccessor (root->left, key);
                if (left)
                {
                        /*if immediate child is key*/
                        if (left->key == key)
                        {
                                return root;
                        }
                        /*tree is returning correct value*/
                        return left;
                }
                tree *right = inorderSuccessor (root->right, key);
                return right;
        }
        return NULL;
}

- SK January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int floor(int val, node x)
{
	if(x==null)
		return null
	if(val>=x.val)
	{
		a = floor(val, x.right)
		if(a==null)
			return x.val
	}
	else
	{
		return floor(val, x.left)
	}
}

- anon123 January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Logic:
There can be three possibilities:
1. There is only one element in the tree
2. The key element is the left node of it's parent node
3. The key element is the right node of it's parent node

Approach:
For case one there can be no result possible
For case two parent of key node is the next larger element
For case three parent of parent is the next larger element

Sol:
Perform binary search on the tree to search key element just make sure you are keeping parent and parent's parent in temp var for current node while traversing tree

Time Complexity: nlog(n)
Space Complexity: (2 * logn)

- PKT January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ask how the BST is provided to you
1. If provided as 2 arrays (inorder and preorder) then binary search for element in inorder array and return next element. check for appropriate boundary conditions
2. If provided as object and pointers, assume a node also saves its parent in addition to left and right for convenience:

public abstract class BST<T>
	{
		//Implement binary search
		public abstract Node<T> findNodeUsingBinarySearch(Node<T> root, T targetValue);
		
		public T findInorderSuccessor(Node<T> root, T targetValue)
		{
			Node<T> target =  findNodeUsingBinarySearch(root, targetValue);
			if(target != null)
			{
				if(target.getRight() != null)
				{
					return target.getRight().getData();
				}
				else
				{
					Node<T> successor = findAncestor(target.getParent(), target);
					if(successor != null)
					{
						return successor.getData();
					}
					else
					{
						//last element
					}	
				}
			}
			else
			{
				throw new ElementNotFoundException();
			}
		}

		//find ancestor such that target is in left subtree of the ancestor
		public Node<T> findAncestor(Node<T> parent, Node<T> child)
		{
			if(parent != null)
			{
				if(parent.getLeft().equals(child))
				{
					return parent;
				}
				else
				{
					return findAncestor(parent.getParent(), parent);
				}
			}
			return parent;
		}
	}

- Nik January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction, return left most child of right subtree if the element has a right subtree

- Nik January 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static boolean flag=false;
public static void inOrder(TreeNode root,int judge){
if(root!=null){
inOrder(root.left,judge);
if(flag) {
System.out.println(root.data);
flag=false;
}
if(root.data==judge) flag=true;

inOrder(root.right,judge);
}
}

t4.left=t3;t4.right=t5;
t3.left=t1;t3.right=t2;
t5.right=t7;t7.right=t6;
Integer judge=10;
inOrder(t4, judge);

- TerrorGeek January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is equivalent to the problem of returning the left-most node in the sub-tree with elements greater than the value provided. Here's my attempt to solve it:

TreeNode* find_next_higher(TreeNode* root, const int& value)
{
    if (root == NULL)
        return NULL;

    if (root->left != NULL && root->left->value > value)
        return find_next_higher(root->left, value);

    if (root->value > value)
        return root;

    if (root->right != NULL)
        return find_next_higher(root->right, value);

    return NULL;

}

- donkey code January 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The leftmost node of right subtree

- glebstepanov1992 January 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following should work (code has comments):

public static Integer find(Node root, int n) {
  if (root == null) {
    // invalid, return max int
    return Integer.MAX_VALUE;
  }
  if (root.v <= n) {
    // solution is in the right branch
    return find(root.right, n);
  } else {
    // solution is either the current node or the max found in the left tree
    // we choose the minimum of the two to find the 'closer' largest value
    return Math.min(root.v, find(root.left, n));
  }
}

Running time is O( lg N ).

- Shams January 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the given input type ?
1) Is it preorder of the binary tree and the node value of which we need to find its successor?
or
2) root of the BST and the node value of which we need to find its successor

- Anonymous January 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getNextLargestValue(Node root, int target) {
if(root == null) {
return -1;
}
if(root.val < target) {
return getNextLargestValue(root.right, target);
} else {
if(root.left == null) {
if(root.val != target) {
return root.val;
}
return -1;
} else {
int val = getNextLargestValue(root.left, target);
return val == -1 ? root.val > target ? root.val : -1
: val;
}
}

}

- Infinity December 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int getNextLargestValue(Node root, int target) {
         if(root == null) {
             return -1;
         }
         if(root.val < target) {
             return getNextLargestValue(root.right, target);
         } else {
             if(root.left == null) {
                 if(root.val != target) {
                     return root.val;
                 }
                 return -1;
             } else {
                 int val = getNextLargestValue(root.left, target);
                 return val == -1 ? root.val > target ? root.val : -1
                                : val;
             }
         }
         
     }

- Infinity December 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do an inorder traversal of the tree. The values will be in ascending order. Then do a binary search on resulting array to find the index of given number, and then find the next highest number in constant time.

- copyconstructor January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can figure out the next higher value in the order traversal itself? Why do a separate binary search? InOrder traversal takes O(n) time and on the system stack takes something between O(lgn) for and O(n) space for balanced and skewed BSTs respectively.

- Murali Mohan January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public bool Search(Node Root, int T, ref Node P)
{
if(Root==null) return;

Node C = Root;

while(C!=null);
{
if(C!=null)
{
if(C.left==null)
{
if(C.Val>T && P==null)
{
P=C; return true;
}
C=C.Right;
}
else
{
Node R = C.Left;
while(R.Right!=null && R.Val!=C.Val)
{
R=R.Right;
}
if(R.Right==null)
{
R.Right=C;
C=C.Left;
}
else
{
R.Right=null;
if(C.Val>T && P==null)
{
P=C; return true;
}
C=C.Right;
}
}
}
}
return false;
}

- RT January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Do inorder traversal till you find first higher value then n - 9.

public static Integer find(Node root, int n) {

	if (root == null) {
	    return null;
	}
	Integer result = null;
	result = find(root.left, n);
	if (root.v <= n) {
	    result = find(root.right, n);
	} else {
	    return root.v;
	}

	return result;
}

- thelineofcode January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

only need to find its successor.

- Kk January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

a

- Anonymous January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Do inorder traversal till you find first higher value then n - 9.

public static Integer find(Node root, int n) {

	if (root == null) {
	    return null;
	}
	Integer result = null;
	result = find(root.left, n);
	if (root.v <= n) {
	    result = find(root.right, n);
	} else {
	    return root.v;
	}

	return result;
}

- thelineofcode January 11, 2014 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More