Tuesday, May 17, 2016

#codingexercise
find the least common ancestor of two nodes
Node GetLCA (Node root, int n1, int n2)
{
if (root == null) return null;
if (root.data == n1|| root.data == n2) return root;
var left = GetLCA(root.left, n1, n2);
var right = GetLCA(root.right, n1, n2);
if (left && right ) return root;
if (left != null) return left;
return right;
}

Find middle of linked list
void GetMid(Node root)
{
var slow = root;
var fast = root;
while( fast && fast.next && fast.next.next)
{
slow = slow.next;
fast = fast.next.next;
}
return slow;
}

Print Left View of a tree
void LeftView(Node root)
{
int max = 0;
LeftViewHelper(root, 1, ref max);
}

void LeftViewHelper(Node root, int level, ref int max)
{
if (root == null) return;
if (level > max){
print root.data;
max = level;
}
LeftViewHelper(root.left, level+1, max);
LeftViewHelper(root.right, level+1, max);
}


Subjective  question : Are solaris zones good or bad ? Give examples
Solaris zones or containers are easy and lightweight. They are not like running a full OS on VMWare. It is just enough for OS to be separate but lightweight but we can run dozens even hundreds.
Applications that had virtually unrestricted physical resource in the native solaris OS do not nearly have the same on a  solaris zone so they are not able to transition without breaks in many cases.

#sorted array to a balanced binary search tree
Node GetTree(List<int> A, int start, int end)
{
if (start > end) return NULL;
int n = end-start+1;
int median = (start+end)/2;
var root = new Node();
root.data = A[median];
root.left =  GetTree(A, start, median-1);
root.right = GetTree(A, median+1, end);
return root;
}

No comments:

Post a Comment