Amazon Interview Question
Backend DevelopersCountry: United States
public class NodeScanner {
static class Node{
String key;
String value;
List<Node> children = new LinkedList<>();
Node parent;
public Node(){
key = "";
parent = null;
}
public Node(String key, Node parent){
this.key = key;
this.parent = parent;
}
public void addChild(Node e){ children.add(e); }
private List<String> getChildrensKeys(){
List<String> keys = new LinkedList<>();
children.stream().forEach(e -> keys.add(e.key));
return keys;
}
}
static class Tree{
Node root;
public Tree(){ root = new Node(); }
boolean isEmpty(){ return root.key == ""; }
public Tree(String str){
this();
Scanner sc = new Scanner(str.trim()).useDelimiter("><|>|<");
parse(root, sc);
}
public void walk() { printNode(root); }
private void printNode(Node n){
System.out.printf("<%s>\n%s\n", n.key, n.value);
for(Node child : n.children)
printNode(child);
System.out.printf("</%s>\n", n.key);
}
private void parse(Node cur, Scanner sc){
String v = sc.next();
Node next;
if(cur.key == ""){
cur.key = v;
next = cur;
}else
if(v.matches("^node[0-9]{1,}$")){
Node n = new Node(v, cur);
cur.addChild(n);
next = n;
}else
if(v.matches("^/node[0-9]{1,}$")){
next = cur.parent;
}else{
cur.value = v;
next = cur;
}
if(sc.hasNext())
parse(next,sc);
}
}
/*
*
* <node0>
* 000000
* <node1>
* 11111
* <node3>3333</node3>
* <node2>
* 2222
* <node5>5555555</node5>
* <node6>66</node6>
* </node2>
* </node1>
* </node0>
*
* */
public static void main(String[] args) {
String str = "<node0>000000<node1>11111<node3>3333</node3><node2>2222<node5>5555555</node5><node6>66</node6></node2></node1></node0>";
Tree tree = new Tree(str);
tree.walk();
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class NodeScanner {
static class Node{
String key;
String value;
List<Node> children = new LinkedList<>();
Node parent;
public Node(){
key = "";
parent = null;
}
public Node(String key, Node parent){
this.key = key;
this.parent = parent;
}
public void addChild(Node e){ children.add(e); }
private List<String> getChildrensKeys(){
List<String> keys = new LinkedList<>();
children.stream().forEach(e -> keys.add(e.key));
return keys;
}
}
static class Tree{
Node root;
public Tree(){ root = new Node(); }
boolean isEmpty(){ return root.key == ""; }
public Tree(String str){
this();
Scanner sc = new Scanner(str.trim()).useDelimiter("><|>|<");
parse(root, sc);
}
public void walk() { printNode(root); }
private void printNode(Node n){
System.out.printf("<%s>\n%s\n", n.key, n.value);
for(Node child : n.children)
printNode(child);
System.out.printf("</%s>\n", n.key);
}
private void parse(Node cur, Scanner sc){
String v = sc.next();
Node next;
if(cur.key == ""){
cur.key = v;
next = cur;
}else
if(v.matches("^node[0-9]{1,}$")){
Node n = new Node(v, cur);
cur.addChild(n);
next = n;
}else
if(v.matches("^/node[0-9]{1,}$")){
next = cur.parent;
}else{
cur.value = v;
next = cur;
}
if(sc.hasNext())
parse(next,sc);
}
}
/*
*
* <node0>
* 000000
* <node1>
* 11111
* <node3>3333</node3>
* <node2>
* 2222
* <node5>5555555</node5>
* <node6>66</node6>
* </node2>
* </node1>
* </node0>
*
* */
public static void main(String[] args) {
String str = "<node0>000000<node1>11111<node3>3333</node3><node2>2222<node5>5555555</node5><node6>66</node6></node2></node1></node0>";
Tree tree = new Tree(str);
tree.walk();
}
}
@ ajay.raj HOW MANY INTERVIEWS YOU DO ? I saw you post a lot of questions for different companies!!!
- Mehdi March 27, 2018