Friday, June 10, 2016

#codingexercise
(Yes some more)
Given a string and ing r that stands for rows, fill the columns and then print the string
Abcdefgh and r = 3 would mean
A d g
B e h
C f
Adgbehcf
String printoff ( string input, int r)
{
Stringbuilder ret;
Int count=0;
For  (int start =0; start < r; start++)
For (int I =0; start +I x r < input.length; i++)
{
ret [count] = input [start + I x r];
Count++;
}
Return ret.toString ();
}

In a BST, replace value of each node with the sum of itself and everything greater than itself.
void updateNode(node root, ref int sum)
{
if (root == null) return;
updateNode(root.right, ref sum);
sum = sum + root.data;
root.data = sum;
updateNode(root.left, ref sum);
}
int sum = 0
updateNode(root, ref sum);

Connect ropes of different lengths with minimum cost where cost = Sum of length of two ropes 
Int getmin (list <int> Len)
{
Int cost = 0
Heap h = build_heap(len)
While (h.size () >1)
{
First = extract_min (h)
Second = extract_min (h)
Cost += first + second
H.insert (first+second );
}
Return cost;
}

And one more
Clone a linked list with next and random pointers
Node clone (node root)
{
If (root == null) return root;
Node new = null;
Node prev = null;
For (node cur = root; cur; cur = cur.next)
{
Var n = cur.clone ();
If new == null new = n;
If (prev) prev.next = n;
Prev =n;
}
// point random link of all originals to correspond to new ones
//Point each random link of all new nodes to originals 
// point each random link of all new to random of random.
}

No comments:

Post a Comment