Flipkart Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
import java.util.ArrayList;
import java.util.List;
public class Node {
List<Node> nodelist = new ArrayList<Node>();
public void addNode(Node node){
nodelist.add(node);
}
static int count;
static int max;
//Assuming if a node has a child, path length between them is 1
public static int maxPathLength(Node node){
searchNode(node);
return max;
}
public static void searchNode(Node node){
if(count>max)
max = count;
count++;
for(Node cNode : node.nodelist){
searchNode(cNode);
}
count--;
}
}
- wmg August 21, 2014