Wednesday, October 13, 2010

Data-structures Algorithms

Algorithm 1 :
Swap two integer variables without any Temporary varaible?
Ans :
a = a^b ; b = b^a ; a = a^b;

Ans 2 :
b = ((a - b)+(a = b));

Algorithm 2 :
How to Reverse a String Without Temp?

Ans :
Hint: just like swapping integers
Swap(char *start,char *end)
{
if(length == String length)
return 1;
*start = (*start - * end) - (*start = * end)
length++;
swap(start+1,end+1);
}

Algorithm 3 : Find the Intersection point of Two Lined Lists ?
Ans :
STEP 1 : Get the Difference between the Number of Nodes in both Linked lists.
Suppose it is "X".
STEP 2 : Move the Head Pointer "X" Locations on the Large Linked List.
STEP 3 : Now Traverse parallel the both Linked lists until reaches a Common Intersection Point .
Details : see this link http://geeksforgeeks.org/?p=2405


WOW What a Blog, i Never Seen Such a Blog before:

http://www.techinterview.org/post/526325766/pirates

Algorithm 5 :
Given an Array contains 1 to n-1 elements in that one element Repeat once, find out that , constraints not allowed to use extra memory.
Ans: use a vector of size 'n' and read the each element in the array and set the bit, whether it was there or not for a repeated element the bit can be set by twice.
But this use extra memory.

Alternative Sloution:

STEP 1 : Sum all the elements in the array.
STEp 2 : sum - ((n)(n-1)/2) = Element.

Where n(n-1) / 2 = Sum from n to n - 1;

sum of n numbers = n(n+1) / 2 ;
Here the Numbers are from 1 to n-1 so the sum would be
n ( n-1) / 2

No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...