Tuesday, September 28, 2010

Given a function Random(5), then how to implement a function of random(n)?

Given a function Random(5), which will return a number of {1, 2, 3, 4, 5} by random, then how to implement a function of random(n)?

Two ways to find number is power of 2.

Two ways to find number is power of 2.

n & n-1 == 0
n & -n == n.

Shorted common superstring

Given an array of strings(Say S[]), and a big string (Say B). Now how to find shortest substring from a big string B, which contains all strings (S[] ).

Find kth smallest element in min heap

How to find kth smallest element in min heap in klogk ?. Note it is not klogN.

Multiply array elements without using division operator

suppose there is an array A[N] of N numbers. We have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n).