# Golang - need to solve coding challenge

we are going to solve using go programming language ([url removed, login to view])

1. Develop MEMSQL DB synchronization. Using program we should able to synchronize primary and secondary database and provide high available system.

2. You are given a tree and nay of the leaf node which is given as input is set to fire. And in each unit time all the neighboring nodes of the node which are already in fire also catch fire. Given a tree find the time taken for the whole tree to catch fire.

Example:

4

/\

5 6

/\/\

7 8 9 10

\ \

13 11

\12

Assume the source leaf node : 9

At 1 sec : 6 catches fire

At 2 sec : 10 and 4 catches fire

At 3 sec : 5 catches fire

At 4 sec : 7 and 8 catches fire

At 5 sec : 13 and 11 catches fire

At 6 sec : 12 catches fire

public class Solution {

public static class Node {
int value;
Node left;
Node right;
}

public int timeTilBurns(Node root, Node start) {
if (root == null || start == null) return 0;

Map<Node, Node> nodesAndParents = new HashMap();
findParensForAllNodes(root, null, nodesAndParents);

int time = 0; // current moment

List<Node> currentBurningNodes = new ArrayList() {{ add(start); }};

while (!nodesAndParents.isEmpty()) {

List<Node> newCurrentBurningNodes = new ArrayList();

// infect neighbours
while (!currentBurningNodes.isEmpty()) {

Node currentBurning = currentBurningNodes.remove(0);

Node parent = nodesAndParents.get(currentBurning);

nodesAndParents.remove(currentBurning);
}

time++;
currentBurningNodes = newCurrentBurningNodes;
}

return time;
}

public void findParensForAllNodes(Node node, Node parent, Map<Node, Node> map) {
if (node != null) {
map.put(node, parent);
findParensForAllNodes(node.left, node, map);
findParensForAllNodes(node.right, node, map);
}
}
}
On Approach:

public static void main(String[] args){

T N10 = new T(12, null, null);
T N9 = new T(11, null, null);
T N8 = new T(3, N10, null);
T N7 = new T(10, null, null);
T N6 = new T(9, null, null);
T N5 = new T(8, null, null);
T N4 = new T(7, N8, N9);
T N3 = new T(6, N6, N7);
T N2 = new T(5, N4, N5);
T N1 = new T(4, N2, N3);

int startFire = 9;
height(N1);
int left = catchFire(N1.left, startFire, -1);
if(left == -1)
left = N1.leftHt-1;
else
left = N1.leftHt - left;

int right = catchFire(N1.right, startFire, -1);
if(right == -1)
right = N1.rightHt-1;
else
right = N1.rightHt - right;

System.out.println((left+right+1));

}

public static int catchFire(T node, int startFire, int fireHt){
if(node == null)
return fireHt;

fireHt = catchFire(node.left, startFire, fireHt);
fireHt = catchFire(node.right, startFire, fireHt);

if(node.left != null && node.left.val == startFire)
fireHt = node.height-1;
if(node.right != null && node.right.val == startFire)
fireHt = node.height-1;

return fireHt;
}

public static void height(T node){
if(node == null)
return;
height(node.left);
height(node.right);

if(node.left != null && node.right != null){
node.height = Math.max(node.left.height, node.right.height) +1;
node.rightHt = node.right.height +1;
node.leftHt = node.left.height +1;
}
else if(node.left != null){
node.height = node.left.height+1;
node.leftHt = node.left.height+1;
}
else if(node.right != null){
node.height = node.right.height+1;
node.rightHt = node.right.height+1;
}
}

static class T{
int val;
T left;
T right;
int height;
int rightHt;
int leftHt;

public T(int val, T left, T right){
this.val = val;
this.left = left;
this.right = right;
}
}

Using recursion:
const timeToCatchFire = (stack, cycle = 1, visited = new Set()) => {
const newGen = [];
for(node of stack) {
}
if(newGen.length > 0)
return timeToCatchFire(newGen, cycle + 1, visited);

Taidot: Backend Development, Golang

Tietoa työnantajasta:
( 0 arvostelua ) India

Projektin tunnus: #16128910

## 4 freelanceria on tarjonnut keskimäärin %project_bid_stats_avg_sub_26% %project_currencyDetails_sign_sub_27% tähän työhön

khannanav

We are a team of experts with more than 8 years of rich cloud experience in AWS [login to view URL], we have worked extensively on Machine Learning. We have developed recommendation/statistical engines, web scrapers/crawlers Lisää

₹7777 INR 3 päivässä
(0 arvostelua)
0.0
₹7777 INR 1 päivässä
(0 arvostelua)
0.0
deytps86

Pretty interesting project to solve in go. Can we talk more on this requirement? Thanks

₹10000 INR 3 päivässä
(0 arvostelua)
0.0
₹41111 INR 10 päivässä
(0 arvostelua)
0.0