Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

- Consider the two paths as two linked lists
- find the length of both the lists and get the difference in length
- move the pointer of longer list by the difference and add every node to the path
- move pointers to both the lists till they meet and add nodes to respective paths
- if the meeting point is root add root to both the paths
- display the paths

void printPath (Node node1, Node node2) {
          if(node1 == null && node2 == null)
              return ;
         
         int len1 = 0, len2 = 0;
      
         Node temp = node1;
         
         while(temp != null) {
             temp = temp.parent; 
            ++len1;
         }
         
       temp = node2;

       while(temp != null) {             
              temp = temp.parent; 
             ++len2;
       }

        StringBuffer path1 = new StringBuffer();
        StringBuffer path2 = new StringBuffer();

        if(len1 > len2 ) {              
            for(int i = 0; i < len1 - len2 ; i++) {
                  
                  path1.append(node1.data);
                 node1 = node1.parent;
               }                  
         }else {

               for(int i = 0; i < len2 - len1 ; i++) {
                  
                  path2.append(node2.data);
                 node2 = node2.parent;
               }                   
          }
          
         while(node1 != node2) {
                path1.append(node1.data);
                path2.append(node2.data);     
                node1 = node1.parent;
                node2 = node2.parent;
          }
         
        if(node1 == root) {
                 path1.append(root.data);
                 path2.append(root.data):
        }
        
       System.out.println(path1.toString());
       System.out.println(path2.toString());

 }

- learner January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the two nodes have the same parent?

- Nan July 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Find the LCA of the two nodes.
- If the LCA is the root, then it means that both the nodes have unique path to the root. Print both the paths.

- Else, it means that the nodes have unique path till the LCA node. Print the paths for each of these nodes till the LCA

- Anonymous January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Anonymous ... This is a good approach in my opinion. Just want to confirm that to find the LCA, if the tree is not a BST and just a binary tree, we have to store the nodes traversed in respective arrays for both the first node and the second node and then find the LCA by finding the common path based on the 2 arrays ... am I right

- Anand January 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First store a pointer to first node. Then keep traversing the first node's parent till we hit the root. Note whether we encounter the second node along the way with the "encountered" variable. If we encountered the second node along the way, then we already printed the path from the second node to the root. Otherwise, keep traversing the second node's parent pointer till we either hit the root or encounter the original first node along the way. Advantage of this is that we only traverse both paths once, and we aren't using any additional data structure.

(Didn't really test this code though)

static void findRoot(Node n, Node n2) {
  boolean encountered = false;
  Node tmp = n;
  System.out.println("FIRST");
  while(n != null) {
    if(n == n2)
      encountered = true;
    System.out.println(n.value);
    n = n.parent;
  }

  System.out.println("SECOND");
  while(!encountered && n2 != null) {
    System.out.println(n2.value);
    n2 = n2.parent;
    if(n2 == tmp)
      encountered = true;
  }
}

- Sunny January 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about storing the path's of both the tree in two HashMap and find if a common node exists....if yes then print the hashMap till that node other wise print the whole HashMap

- Anuj Agrawal February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

question not very clear..
what I understand from it.is
it will be a complete binary tree and then from any two nodes ,we can find the shortest path
from the root and can eliminate the common path...

i dont know if it holds correct.

- Abhinav January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I really do not understand what is the meaning of printing path here. My understanding is printing the value or the node pointer.
if so code goes here

printpath(node * node1, node* node2)
{
if(node1 == NULL || node2 == NULL)
{// reached parent
return;
}
if(node1 == node2)
{
// now both the nodes have same parent so stop printing
return;
}
print node1;
print node2;
printpath(node1-> parent, node2->parent);
}

- dfog January 03, 2013 | 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