## Amazon Interview Question for SDE-2s

• 0

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) {

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;
}

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

return null;
}

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

List<Node> children = node.children;

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

return;
}
}

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) {

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;
}

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

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();

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;
}
}``````

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.

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.

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.

### 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.