Tuesday, July 5, 2016

#codingexercise
Find the longest repeated substring in a string.
We build a suffix tree to keep track of substrings encountered so far
int suffixesleft = 0;
int leafEnd = -1;
lastNewNode = null;
void extendSuffixTree(int pos)
{
leafEnd = pos;
suffixesleft ++;
lastNewNode = null;
while (suffixesleft > 0){
if (activeLength == 0)
    activeLength = pos;
if (activeNode.children[text[activeEdge]] == NULL)
     activeNode.children[text[activeEdge]] = newNode(pos, &leafEnd);
     if (lastNewNode != null){
          lastNewNode.suffixLink = activeNode;
         lastNewNode = null;
    }
}
else{
    var next = activeNode.children[text[activeEdge]];
    if (walkDown(next)) continue;
    if (text[next.start + activeLength] == text[pos])
    {
        if (lastNewNode != null && activeNode != root)
        {
             lastNewNode.suffixLink = activeNode;
             lastNewNode = null;
         }
         activeLength++;
         break;
    }
var splitEnd = next.start + activeLength -1;
splitEnd = next.start + activeLength -1;
var split = newNode(next.start, splitEnd);
activeNode.children[text[activeEdge]] = split;
split.children[text[pos]] = newNode(pos, ref leafEnd);
next.start += activeLength;
split.children[text[next.start]] = next;
if (lastNewNode != null){
lastNewNode.suffixLink = split;
}
lastNewNode = split;
}
suffixesleft--;
if (activeNode == root && activeLength > 0)
{
     activeLength--;
     activeEdge = pos - remainingSuffixCount  + 1;
}else if (activeNode != root){
    activeNode = activeNode.suffixLink;
}
}
}

Build a binary search tree from postorder traversal
node treebuilder(List<int> post, ref int index, int key, int min, int max, int size)
{
if (index < 0)
   return null;
node root = null;
if (key > min && key < max)
{
   root = newNode(key);
   index = index - 1;
   if (index > 0)
    {
      root.right  = treebuilder(post, index, post[index], key, max, size);
      root.left = treebuilder(post, index, post[index], min, key, size);
     }
}
return root;
}
courtesy : geeksforgeeks

Given a set of integers, negative as well as non negative, rearrange them such that negative and non negative integers are at alternate positions.
void rearrange(ref List<int> nums)
{
int neg = -1;
int pos = -1;
for (int i =0; i < nums.count; i++)
{
if (i%2 == 0){
neg = get_next_neg(nums, i);
if (neg != -1)
{
temp = nums[neg];
shift_right(nums, i, neg);
nums[i] = temp;
}else{
break;
}
} else{
pos = get_next_pos(nums, i);
if (pos != -1)
{
temp = nums[pos];
shift_right(nums, i, pos);
nums[i] = temp;
}else{
break;
}
}
}
}


int binary_search(List<int> nums, int start, int end, int val)
{
int mid = (start + end)/2;
if (nums[mid] == val) return mid;
if (start == end && nums[mid] != val) return -1;
if (nums[mid] < val)
return binary_search(nums, mid+1, end, val);
else
return binary_search(nums, start, mid, val);

}
Check whether any permutation of the inout string is a palindrome.
Bool isPalindrome(strimg input)
{
Var h = new Hashtable();
For (int i =0; i < input.length; i++)
{
If h.keys.contains(input[i]){
h[input[i]] += 1;
}
Else{
h.Add(input[i], 1);
}
}
Var counts = h.values.toList().distinct().sort();
if counts.count == 1 && counts[0] == 2 return true;
If (h.values.count.all ( x => x %2 == 0) return true;
If (h.values.count( x => x % 2 == 1) == 1 ) return true;
Return false;
}



No comments:

Post a Comment