Zoho Interview Question
Senior Software Development EngineersTeam: Theriyathu
Country: India
Interview Type: In-Person
It is simple if we make father childList Map.
1. Create Map<Father, List<Child>> by iterating over pairs.
2. For level =1 map.get(father) which in turn returns childList (List<Child>) totalChildren = childList.size()
3. For level = 2 , Iterate over childList and each element of childList is now father so map.get(childList(i)) and add the size of return value for totalChildren.
repeat same for the next level.
function getChildren (map, name, level) {
if (level === 1) {
return map[name]
}
var children = []
for (var i = 0; i < map[name].length; ++i) {
children = children.concat(getChildren(map, map[name][i], level - 1));
}
return children;
}
module.exports = function (A, name, level) {
var map = {}
for (var i = 0; i < A.length; ++i) {
var child = A[i][0];
var father = A[i][1];
map[father] = map[father] || [];
map[father].push(child);
}
var children = getChildren(map, name, level);
return [children, children.length];
}
var children = module.exports([
['Ram', 'Syam'],
['Akil', 'Syam'],
['Nikil', 'Ram'],
['Subhash', 'Ram'],
['Karthik', 'Akil']
], 'Syam', 2);
console.log(children);
import java.util.ArrayList;
import java.util.List;
public class PrintChildCound {
int countChildLevel(Node root, int level, int currentLevel, int total) {
if(root == null) {
return 0;
}
if(level == currentLevel) {
System.out.println(root.val);
total++;
}
for(Node node : root.list) {
total = countChildLevel(node, level, currentLevel+1, total);
}
return total;
}
public static void main(String... args) {
Node shyam = new Node("Shyam");
Node akhil = new Node("Akhil");
shyam.hasChild(akhil);
Node ram = new Node("Ram");
shyam.hasChild(ram);
ram.hasChild(new Node("Nikil"));
ram.hasChild(new Node("Subhash"));
akhil.hasChild(new Node("Karthik"));
System.out.println(new PrintChildCound().countChildLevel(shyam, 2, 0, 0));
}
}
class Node {
Node(String val) {
this.val = val;
}
void hasChild(Node node) {
list.add(node);
}
String val;
List<Node> list = new ArrayList();
}
import java.util.*;
public class NumOfChildrenImpl{
Map<String,String> relationships;
public NumOfChildrenImpl(Map<String,String> relationships){
this.relationships = relationships;
}
public int getNumOfChildren(String fatherName,int level){
Set<String> directChildren = new HashSet<>();
for(Map.Entry<String,String> entry:relationships.entrySet()){
if(entry.getValue().equals(fatherName)){
directChildren.add(entry.getKey());
}
}
if(level<=1){
//for(String childName:directChildren){
// System.out.print(" " + childName + " ");
//}
return directChildren.size();
}
int numOfIndirectChildren=0;
for(String childName:directChildren){
numOfIndirectChildren+=getNumOfChildren(childName,level-1);
}
return numOfIndirectChildren;
}
}
{{
function go(name, level) {
var input = [
['aa', 'bj'],
['bb', 'bj'],
['cc', 'wk'],
['bj', 'kong'],
['wk', 'kong'],
];
var tree = {};
for(var i in input) {
var child = input[i][0];
var parent = input[i][1];
if(!tree[parent]) {
tree[parent] = {};
}
if(!tree[parent][child]) {
tree[parent][child] = {};
}
}
var count = 0;
var curKeys = [name];
while(count < level) {
var keys = [];
for(var i in curKeys) {
keys = keys.concat( Object.keys(tree[curKeys[i]]) );
}
curKeys = keys;
count++;
}
return curKeys.length;
}
}}
#include<iostream>
#include<string>
using namespace std;
struct s
{
string child;
string parent;
};
int find(s cp[],string name,int level,int l,int n)
{
int i;
int count=0;
if(l==level)
return 1;
for(i=0;i<n;i++)
{
if(cp[i].parent==name)
{
count=count+find(cp,cp[i].child,level,l+1,n);
}
}
return count;
}
int main()
{
int n,i,level,l=0,count=0;
cin>>n;
s cp[n];
string name;
for(i=0;i<n;i++)
{
cin>>cp[i].child;
cin>>cp[i].parent;
}
cin>>name>>level;
cout<<find(cp,name,level,l,n);
}
- Bradley Wheeler December 19, 2016