Amazon Interview Question for SDE-2s


Country: United States




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

/*
recursive solution : search a given node. Once found, add a currently visited node to result and call a method itself recursively for every child of its children. Repeat the steps until it visits all the nodes.
time O(n) where n is the total number of nodes in a n-ary tree/trie.
space O(n) in the worst case when a trie is left-skewed and a call stack would have n number of method calls.
*/
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> result = new LinkedList<>();

Node target = new Node(3);
root = findNode(root, target, result);

preorderUtil(root, result);

return result;
}

private Node findNode(Node node, Node target, List<Integer> result) {
if (node.val == target.val) {
return node;
}

Queue<Node> q = new LinkedList<>();
q.add(node);

while (!q.isEmpty()) {
Node temp = q.remove();
if (temp.val == target.val) {
return temp;
}
else {
List<Node> children = temp.children;
for (Node child : children) {
q.add(child);
}
}
}

return null;
}

private void preorderUtil(Node node, List<Integer> result) {
if (node == null) {
return;
}

result.add(node.val);

List<Node> children = node.children;

for (Node child : children) {
preorderUtil(child, result);
}

return;
}
}

- Euihoon Seol October 17, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
iterative : search a given node by using a queue. Once found, use a stack to iterate through the trie. Add currently visited node to the stack. pop it out, add it to the result list and add its children back to the stack in a reverse order.
time O(n) where n is total number of nodes in a trie
space O(n) in the worst case when a trie is two-level
*/

class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> result = new LinkedList<>();
        
        Node target = new Node(3);
        root = findNode(root, target, result);
        
        preorderUtil(root, result);
        
        return result;
    }
    
    private Node findNode(Node node, Node target, List<Integer> result) {
        if (node.val == target.val) {
            return node;
        }
        
        Queue<Node> q = new LinkedList<>();
        q.add(node);
        
        while (!q.isEmpty()) {
            Node temp = q.remove();
            if (temp.val == target.val) {
                return temp;
            }
            else {
                List<Node> children = temp.children;
                for (Node child : children) {
                    q.add(child);
                }
            }
        }
        
        return null;
    }
    
    private void preorderUtil(Node node, List<Integer> result) {
        Stack<Node> s = new Stack<>();
        s.push(node);
        
        while (!s.empty()) {
            Node temp = s.pop();
            result.add(temp.val);
            
            List<Node> children = temp.children;
            Stack<Node> tempS = new Stack<>();
            
            for (Node child : children) {
                tempS.push(child);
            }
            
            while (!tempS.empty()) {
                s.push(tempS.pop());
            }
        }
        
        return;
    }
}

- Euihoon Seol October 17, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we convert our n-ary representation from

class Node{
int data
List<Node>childre;
}

to :

class Node{
int data;
Node firstChild;
Node nextSibling;
}

Then, our n-ary tree becomes a simple binary tree.
We can easily traverse the tree to find the node by making use of firstChild(consider as left Pointer) and nextSibling (consider as right Pointer)
Also,preOrder Traversal can be done, following something like: root, firstChild, nextSibling.

- Somya Saxena December 23, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we convert our n-ary representation from

class Node{
int data
List<Node>childre;
}

to :

class Node{
int data;
Node firstChild;
Node nextSibling;
}

Then, our n-ary tree becomes a simple binary tree.
We can easily traverse the tree to find the node by making use of firstChild(consider as left Pointer) and nextSibling (consider as right Pointer)
Also, preOrder Traversal can be done, following something like: root, firstChild, nextSibling.

- somyasaxena23 December 23, 2020 | 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