Saturday, June 4, 2016

#codingexercise
Void PrintBinaryNumbersUpto(int N) 
{ 
Var ret = new List<Bit>(); 
For (int I =0; I < N; I++) 
{ 
If (I == 0)  {ret.add(new Bit(0)); continue;} 
If (ret[ret.Count-1] == 0) {ret[ret.Count-1] = 1; continue;} 
For (int I = ret.Count -2; I >=0; i++) 
{ 
If (ret[i] == 0) {ret[I] == 1; break;} 
ret[I] = 0; 
If (I == 0){ret.InsertAt(I, new Bit(1)); break;} 
} 
Console.Write(ret.toString()); 
} 
} 

A Linked list ends in a loop. Remove the loop and flatten the list. 
Void RemoveLoop(node root) 
{ 
If (root == null) return; 
Node loopnode = DetectLoop(root); 
If (loopnode == null) return; 
For (node cur = root; cur.next != loopnode; cur = cur.next) 
{ 
//traverse the loop to see if it encounters cur 
For (node next = loopnode.next; next.next != loopnode.next; next = next.next) 
{ 
If (next.next == cur){next.next = null; break;break;} 
} 
 
} 
Node DetectLoop(Node root) 
{ 
If (root == null || root.next == null) return;  
Node slow = root; 
Node fast = root; 
While ( slow && fast  && fast.next){ 
Slow = slow.next; 
Fast = fast.next.next; 
If (slow == fast) return slow; 
} 
Return null; 
} 
#codingquestion
Void printLeftView (node root, int level, ref int minLevel)
{
If (root==null)return root;
If (level > maxlevel)
     maxlevel = level;
PrintLeftView (root.left, level+1, maxlevel);
PrintRightView (root.right, level+1, maxlevel);


}

Void printRightView (node root, int level, ref int maxLevel)
{
If (root==null)return root;
If (level < maxLevel)
     MaxLevel = level;
PrintLeftView (root.right,  level+1, maxlevel);
PrintRightView (root.left, level+1, maxlevel);
}

Duplicity software takes different backends as remote source for file CRUD operations. . More about it here: https://goo.gl/f9xoiq

No comments:

Post a Comment