Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

def recur_clone(tree):         
    def __recur_clone(root):  
        if not root:
            return None 
        node = BT._Node(root.val)
        node.left = __recur_clone(root.left)
        node.right = __recur_clone(root.right)
        return node
    ct = BT()
    ct.root = __recur_clone(tree.root)    
    return ct

- duke April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node {
public string Value{get;set}
public Node Left {get;set;}
public Node Right{get;set;}
}


Node CloneNode(Node n1)
{

if(n1 != null)
{
Node c = new Node{
Value = n1.Value
};
c.Left = CloneNode(n1.Left);
c.Right = CloneNode(n1.Right);
}
}

- Fits March 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cloning with recursion is trivial. Was it asked to be done without recursion ?

- GImmeaJobPlz March 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node  *clone( root ) 
{
   if (root == NULL ) : return root;
 
   //create new node and make it a copy of node pointed by root
   node *temp = (node *)malloc(sizeof(node)) ;
   temp->data = root-> data;    //copying data
   temp->left = clone(root -> left);    //cloning left child
   temp->right = clone(root -> right);  //cloning right child
   return temp;
}

- ajkool111 March 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

Node* clone(const Node* node)
{
    Node* new_node = nullptr;
    
    if(node)
    {
        new_node = new Node { nullptr, nullptr, node->val };
        
        new_node->left  = clone(node->left);
        new_node->right = clone(node->right);
    }
    
    return new_node;
}

- Leonid April 25, 2018 | 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