Amazon Interview Question


Country: United States
Interview Type: Phone Interview




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

All answers so far are incorrect. What you are looking for here is the "successor" of the current node. For the given question, I assume all nodes have a pointer two its parent. Otherwise, we need a O(N^2) solution.

There are two cases:
(1) the current node has a right subtree
(2) the right subtree of the current node is NULL.

For (1), the successor is the leftmost child of the right subtree
For (2), we have to go up towards the root till we find a parent node, whose left subtree contains the current node.

Also, note that the node might be the largest element in the tree, and hence there is no successor. In this case, the code returns NULL.

here is the pseudo code:

successor(x) {
    if (x -> right != NULL)     // current node DOES have a right node
        y = x -> right;
        while (y.left != NULL)  // return the minimum element of the right sub-tree
            y = y -> left;
        return y;
    }
    else { // current node does NOT have a right subtree
        y = x -> parent;
        while (y != NULL && x ==y -> right) {   // go up the tree 
            x = y;
            y = y -> parent;
        }
        return y;
    }

- gokayhuz February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In the part where node does have a right child, there is a bug in your code.

If the right child has null as left child your code would return null whereas it should return the right child itself.

- aj February 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In fact, if the node does not have a parent pointer we can solve it in O(n) time. Just add one extra flag to recursion and set it to true when you see the number whose successor is desired. That would be O(n).

void successor(int &flag, node *root, int num)
{
	if(root == NULL)
		return;
	successor(flag, root->left);
	if(root->data == num)
		flag = 1;
	if(flag)
		cout << root->data;
	successor(flag, root->right, int num);		
}

If it does have a parent pointer, in a balanced bst we would be spending O(logn) time. But in the worst case(tree is not balanced), we might spend O(n).

- aj February 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanx aj. I fixed the "little" bug. It should be working now :)

However, your code also has one bug. More importantly, the little bug, I do not think it works.

The bug is when you call

successor(flag, root->left);

The function should take 3 parameters, not one.

And about the "real" problem...

Imagine a tree, where root has the value 10. To the left of the node, we have a child with data of 7. And 7 has a right child with data 9. Now, the successor of 9 is the root node, 10. However your code only prints 9 and not the successor

- gokayhuz February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi gokayuz.

Thanks for pointing out. Apologies for the sloppiness. I thought you would get the point. Here is the working code.

void successor(int &flag, BST *root, int num)
{
  if(root == NULL)
    return;
  successor(flag, root->left, num);
  if(root->key == num)
    flag = 1;
  else if(flag)
    cout << root->key;
  successor(flag, root->right, num) ;
}

call this as

int i = 0;
successor(i, root, 9)

- aj February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The previous code would in fact print all the successors. Just to print one you would have to modify else if part a bit.

void successor(int &flag, BST *root, int num)
{
  if(root == NULL)
    return;
  successor(flag, root->left, num);
  if(root->key == num)
    flag = 1;
  else if(flag){
    cout << root->key;
    flag = 0;
  }
  successor(flag, root->right, num) ;
}

- aj February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

successor is next highest, the task is to find next least

- Anonymous February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"next least", as I understand, is the node that is the smallest that comes after the current node. So, it is asking for the successor.

But you are right, there is some ambiguity about the wording and this needs to be clarified at a interview. But, the given solutions are easy to implement correctly if the question is clarified.

- gokayhuz February 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Climb the tree. Go to the node. See where the leaves are pointing. Go there and yes, you have found the solution.

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

The next least node is either the right child or the leftmost child in the right subtree

- WinCPP February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node nextLeast(Node n) {
node left = n.left;
while (left.right != null) {
left = left.right;
}
return left;
}

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

n.left?

- mo February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct tree TREE;

struct tree {
	int val;
	struct tree *left;
	struct tree *right;
};

typedef struct tree TREE;

TREE* makenode(int val) {
	
	TREE* node = (TREE *)malloc(sizeof(struct tree *));
	node->val = val;
	node->right = NULL;
	node->left = NULL;
	
	return node;
}

int findNextLeast(TREE* root, int n){
	
	if(root == NULL)
		return -1;
	
	if(root->val == n)
		return n;
		
	int l = findNextLeast(root->left, n);
	if(l == n)
		return root->val;
	
	int r = findNextLeast(root->right, n);
	if(r != -1)
		return r;
	
	return -1;
	
}

int main () {
	
	TREE * root = makenode(10);
	root->left = makenode(5);
	root->right = makenode(15);
	
	root->left->left = makenode(2);
	root->left->right = makenode(8);
	
	root->right->left = makenode(12);
	root->right->right = makenode(17);
	
	int ret = findNextLeast(root, 8);
	
	if(ret != -1)
		printf("\nThe next least : %d", ret);	
	
	
	
	return 0;
}

- SBURookie February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume we have parent pointer to the node.

public static BSTNode GetNextLeastNode(BSTNode node)
{
    if(node == null)
        return null;

    if(node.Right != null)
    {
        node = node.Right;
        while(node.Left != null)
        {
            node = node.Left;
        }
        return node;
    }
    
    if( node.Right == null)
    {
        while(node.Parent != null && node.Parent.Parent != null && node.Parent.Parent.Right == node.Parent)
        {
            node = node.Parent;
        }
        if(node.Parent != null && node.Parent.Parent != null )
        {
            return node.Parent.Parent;
        }
        else
        {
            return null;
        }
    }
}

- Anonymous February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"next least node in a binary search tree. "

In binary search tree this should be the next node while doing in-order traversal.

- Anonymous February 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone just explain what next least node means in simple words before giving any code. Giving an example is important even in interviews. It will also be helpful to people here. Thanks!

- Anonymous February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I tried to define what the "next least node" is in my solution. in this question, the "next least node" is the node that comes after a specific node in an "in-order traversal of the tree". It is usually called the "successor" of the node.

In layman's terms, if a tree contains nodes with the key values {3, 7, 19 ,23, 26, 31, 40}, the next least node for node{23} is node{26}. For node{3}, the successor is node {7} and naturally node{40} does not have a successor

- gokayhuz February 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the solution should be to traverse the tree using inOrder traversal
1. Store this traversal in an array arr[]
2. get the element mentioned in the problem
3. The number at the position i-1 is the next least number..

- kavita February 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node *getOneLastLeast(Node* root){

if(root == NULL)
return NULL;

if( heightBST(root->left) == 1)
return root;
else
return ( getOneLastLeast(root->left));
}

- Jay February 26, 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