GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer (More than 5 Years)

(11 posts)

Tags:

  1. Manu
    guest
    Posted 9 months ago #

    A Binary tree ( Need not be BST) node has three pointers. Left child, right child and parent pointer. Another pointer is added called ExtraID which points to either NULL or one of the inorder successor. Inorder successor means , if we list nodes in InOrder, Inorder successor is a valid node on right side of the node in the list. Find an O(n) algorithm to analyze a given binary tree and identify nodes with invalid ExtraIDs.

  2. manu
    Member
    Posted 8 months ago #

    No takers for this ?

  3. WgpShashank
    guest
    Posted 8 months ago #

    Hi Manu, Check For each Node if extra_Id node is pointing to its inorder successor or not so problem reduces to find inorder successor for each node in tree & we can keep an array to put the all the nodes for which theri extra_id is not pointing to inorder successor , i hope you can easily write code for this

    correct me if i missed something ?

    Shashank !!!

  4. camster
    Member
    Posted 5 months ago #

    WgpShashank, It is possible that to find the inorder-successor of an node in the binary tree, the best case is O(log N) -- worst case is O(N)? So, if we have N nodes in the binary tree , then the worst case cost of your solution might 0(N log N) or 0( square(N))).
    Is it possible that you could could keep track of the in-order successor for each binary tree node as you traverse through the binary tree so that you get O(N) cost?
    Also, the parent pointers of each binary tree node contain potentially valuable information which you might be able to exploit? Thank you. Camster.

  5. camster
    Member
    Posted 5 months ago #

    WgpShashank, Here is a potential O(N) - order(N) to Manu's question. I will post the test program later today. Thank you.

    void hmary(nodemaryc* node){
    if (node == NULL)
    return;

    hmary(node->left);
    if (node->right != NULL && node->extraId != NULL && node->right != node->extraId){
    printf("Not valid %d\n",node->value);
    }
    hmary(node->right);

    }

  6. camster
    Member
    Posted 5 months ago #

    Manu and WgpShashank, Here is the proposed O(N) solution to the interview question followed by a test program. Please tell me whether is what you are looking for. (Frank Chang)

    void hmary(nodemaryc* node){
    	if (node == NULL)
    		return;
    
    	hmary(node->left);
    	if (node->right != NULL && node->extraId != NULL &&
                              node->right->value != node->extraId->value){
    		printf("Not valid %d\n",node->value);
                         }
    	hmary(node->right);
    
    }
    
    int main(int argc,char** argv[]){
    
      nodemaryc* node6;
      nodemaryc* node4;
      nodemaryc* node7;
      nodemaryc* node5;
    
      node4 = new nodemaryc;
      node4->value = 4;
      node4->left = NULL;
    
      node4->extraId = NULL;
    
     node7 = new nodemaryc;
      node7->value = 7;
      node7->left = NULL;
      node7->right = NULL;
    
      node7->extraId = NULL;
    
      node6 = new nodemaryc;
      node6->value = 6;
      node6->left = node7;
      node6->right = node4;
      node6->parent = NULL;
    
      node5 = new nodemaryc;
      node5->value = 5;
      node5->left = NULL;
      node5->right = NULL;
      node5->parent = node4;
      node5->extraId = NULL;
    
     node5 = new nodemaryc;
      node5->value = 5;
      node5->left = NULL;
      node5->right = NULL;
      node5->parent = node4;
      node5->extraId = NULL;
    
      node4->parent = node6;
      node7->parent = node6;
    
     node6->extraId = node5; // node4
    
      node4->right = node5;
    
       hmary(node6);
    
      return 1;
    }
  7. anamika sharma
    Member
    Posted 5 months ago #

    make an array which indexes the nodes. Initialise it with all 0s. Now traverse the tree in inorder. While traversing
    1) make the corresponding value of this node in the array as 1
    2) chk if the node's ID is pointing to a node whose corresponding value in the array is 0. If the value is 1 den it is an invalid ID.
    Correct me if I m wrong

  8. Atul Bajpai
    guest
    Posted 5 months ago #

    @Camster: I think ur code is not covering leaf nodes as the ExtraID for leaf node could be null so as the inorder successor. Correct me if i m wrong.

  9. camster
    Member
    Posted 5 months ago #

    @Atul, Please provide with an example of a binary tree with the leaf node(s) you are referring to. Thank you.

  10. camster
    Member
    Posted 5 months ago #

    @Atul, Here is a program that solves the bug you found in my program. Thank you for pointing out the mistake.

    nodemaryc* InOrderSuccessor(nodemaryc* test){
    
    	nodemaryc* tmp = test;
    	while (tmp->parent != NULL){
    		tmp = tmp->parent;
    	}
    	if (tmp != test){
    		return tmp;
    	}
    	else{
    		return NULL;
    	}
    }
    
    void hmary(nodemaryc* node){
    	nodemaryc* IOSuccessor;
    	if (node == NULL)
    		return;
    
    	hmary(node->left);
    	if (node->right != NULL && node->extraId != NULL &&
                              node->right->value != node->extraId->value)
    		printf("Not valid %d\n",node->value);
    	else if (node->right == NULL && node->extraId != NULL){
    		IOSuccessor = InOrderSuccessor(node);
    		if(IOSuccessor == NULL || IOSuccessor->value != node->extraId->value)
                                                    printf("Not valid %d\n",node->value);
    	}
    	hmary(node->right);
    
    }
  11. camster
    Member
    Posted 5 months ago #

    @Atul, Here is a better InOrderSuccessor function. Thank you

    nodemaryc* InOrderSuccessor(nodemaryc* test){

    nodemaryc* tmp = test;
    while (tmp->parent != NULL){
    tmp = tmp->parent;
    }
    if (tmp != test && tmp->value > test->value){
    return tmp;
    }
    else{
    return NULL;
    }
    }


Reply

You must log in to post.

RSS feed for this topic