Friday, September 11, 2009

AlgorithmhtiroglA

1...Find the K'th Maximum Element in a Binary Search Tree . Do it in O(log N).

2...Find a two-line program to output the Nth Fibonacci number.

3...Given text, count the words inside.
you are reminded ".", ",", "?" could also be separator, plus number like "123,456,789.012" where '.'/',' not considered as separator...
The question is more about requirement and definition.
E.g. how many words in the string below?
"ab $2,300.58?2,,300.58 123?.235"

4...Find the longest palindromic substring in a string.

5...Given a n*m matrix having numbers such that each row and each column sorted. Now print the numbers in present in the matrix in sorted order.

6...Given a binary tree, prove that it is a BST(Binary search tree), without using inorder traversal either recursively or iteratively..?

7...Given an n*n matrix A. Each row of the matrix and each column of the matrix is sorted.
Given a number S, you are required to find S in the matrix( report k, l such that A[k,l] = S) or conclude S is not in A.
How can you solve it in O(n)?

8...A String as given as input in the form of Hex Decimal format and you need to print the ASCII format.

9...Print a binary tree in zig zag way... that is:
......a.........
....b....c.......
..d...e...f...g..
.h.i.j.k.l.m.n.o.

should be printed as a-c-b-d-e-f-g-o-n-m-l-k-j-i-h

what data structure will u use for that?

10...Write a function to reverse a line(i.e "How are you" will become "you are How")

11...give you two strings, one source string, one pattern string, check if they are totally match. The pattern string could include '?' which can replace one character. The pattern string could also include '*' which can replace multiple (0-n) characters. For example,
1. src = "abcde", pattern = "ab?de", return true.
2. src = "abcde", pattern = "a*d?", return true.
3. src = "abcde", pattern = "*fg", return false.

12...Given an integer n. Create an array of elements between 1..n in random order in the array. The probability of choosing a number for any position in the array should be the same.

13...How to identify bits that alternate 1 and 0. Ex: 101010, 10101, 01010, 01.

15...You have a random 5 function. Generate a random 7 function using this random 5 function with equal probability

3 comments:

Anonymous said...

ll this do????
long fib(int n)
{
return n<2 ? n : fib(n-1) + fib(n-2);
}

Anonymous said...

for reversing a string can we do like dis????

1.Reverse it using string reverse function(EG:I LOVE INDIA as AIDNI EVOL I)
2Then reverse each word.(INDIA LOVE I)...

If anything better???
Or an idea to implement this with least time????

Anonymous said...

For alternate bit finding......

1.Do Right shift
2.Exor the bit shifted out with 1.
3.The result ld be true and false alternatively!!!

Bit confusing!! Think dis wont work!!
post it if anybody have an idea!!

Does any1 know "is there a way to store the shifted out bits"????