Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Hey, thanks a ton for this. Trying to go thru the logic, but a little difficult to undersatnd without comments for someone new to collections. Could you plese comment on the time complexity of this and Could you please port it to the following function and interface so that i can test it.
Thanks a ton!
method name: void displayBFSReverse (Node head);
Where Node implements the interface:
interface Node {
public String getNodeData ();
public List<Node> getChildren ();
}
Starting to sound a little like homework...
(Honestly, in all fairness, if you're new to collections, this problem is probably not the first problem you should be attempting.)
Use a set to hold the nodes , starting from root node for each level obtain the list of nodes and add them to stack or queue . Push the queue or stack to the set defined initially ,
After traversing all the levels , retrieve elements from the set starting from the last element and print them
public void displayTree_BreadthFirst(Node root) {
Queue<Node> queue = new LinkedList<Node>();
Stack<Node> stack = new Stack<Node>();
queue.add(root);
stack.push(root);
while (!queue.isEmpty()) {
Node node = queue.poll();
System.out.printf("%3d", node.element);
if (node.left != null) {
queue.add(node.left);
stack.push(node.left);
}
if (node.right != null) {
queue.add(node.right);
stack.push(node.right);
}
}
System.out.println();
while (!stack.isEmpty()) {
System.out.printf("%3d", stack.pop().element);
}
}
Output
=====
Regular = 1 2 3 4 5 6 7
Reverse = 7 6 5 4 3 2 1
Was the question asked to use constant space OR can a queue be used OR can a queue and stack be used ?? Was there any limitation on available space?
Its a written question. The question says that one needs to reverse the tree in BFS.
The Tree Structure is
34
/ | \
3 56 12
/ \ |
89 7 22
|
78
reverse in BFS and return should return
78 22 7 89 12 56 3 34
Comment on the time complexity of the problem and
The code need to fit the following method and interface.
method name: void displayBFSReverse (Node head);
Where Node implements the interface:
interface Node {
public String getNodeData ();
public List<Node> getChildren ();
}
as previous commenter said, really depends on the space constraints. below code in c++.
int ReverseBFS(Node* root) {
vector<Node*> nodes;
int i=0;
nodes.push_back(root);
while (i < nodes.size()) {
for (int j=0;j<nodes[i]->children.size();j++) {
nodes.push_back(nodes[i]->children[j]);
}
i++;
}
for (int i=nodes.size()-1;i>=0;i--) {
cout << nodes[i]->value << " ";
}
cout << endl;
return 0;
}
if there'll be back, forward or cross edges, need to maintain a separate visited array.
package com.algorithms;
import java.util.ArrayList;
import java.util.List;
public class BFS {
private static List<String> list = new ArrayList<String>();
public static void main(String[] args){
BFSNode rootNode = new BFSNode("1");
List<BFSNode> childrenNodeListFirst = new ArrayList<BFSNode>();
childrenNodeListFirst.add(new BFSNode("2"));
childrenNodeListFirst.add(new BFSNode("3"));
childrenNodeListFirst.add(new BFSNode("4"));
rootNode.childrenNodeList = childrenNodeListFirst;
BFSNode childNode = childrenNodeListFirst.get(0);
List<BFSNode> childrenNodeListSecond = new ArrayList<BFSNode>();
childrenNodeListSecond.add(new BFSNode("5"));
childrenNodeListSecond.add(new BFSNode("6"));
childNode.childrenNodeList = childrenNodeListSecond;
childNode = childrenNodeListFirst.get(2);
List<BFSNode> childrenNodeListFourth = new ArrayList<BFSNode>();
childrenNodeListFourth.add(new BFSNode("7"));
childrenNodeListFourth.add(new BFSNode("8"));
childNode.childrenNodeList = childrenNodeListFourth;
childNode = childrenNodeListSecond.get(0);
List<BFSNode> childrenNodeListFifth = new ArrayList<BFSNode>();
childrenNodeListFifth.add(new BFSNode("9"));
childrenNodeListFifth.add(new BFSNode("10"));
childNode.childrenNodeList = childrenNodeListFifth;
childNode = childrenNodeListFourth.get(0);
List<BFSNode> childrenNodeListSeventh = new ArrayList<BFSNode>();
childrenNodeListSeventh.add(new BFSNode("11"));
childrenNodeListSeventh.add(new BFSNode("12"));
childNode.childrenNodeList = childrenNodeListSeventh;
List<BFSNode> queue = new ArrayList<BFSNode>();
queue.add(rootNode);
BFSTraverse(queue);
for(int i=list.size()-1;i>=0;i--){
System.out.println("Node : "+list.get(i));
}
}
public static void BFSTraverse(List<BFSNode> queue){
while(!queue.isEmpty()){
BFSNode node = dequeue(queue);
list.add(node.nodeName);
//System.out.println("Node : "+node.nodeName);
if(node.childrenNodeList!=null){
enqueue(node.childrenNodeList,queue);
}
BFSTraverse(queue);
}
}
public static void enqueue(List<BFSNode> childNodeList,List<BFSNode> queue){
for(int i=0;i<childNodeList.size();i++){
queue.add(childNodeList.get(i));
}
}
public static BFSNode dequeue(List<BFSNode> queue){
BFSNode node = queue.get(0);
queue.remove(0);
return node;
}
}
class BFSNode{
List<BFSNode> childrenNodeList;
String nodeName;
public BFSNode(String nodeName){
this.nodeName = nodeName;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
}
Use queue to traverse level by level and stack to hold all elemnts in reverse order.
- m@}{ June 08, 2012