1. largest contiguous sub-array
1 0 1 1 1 0 0 0
0 0 0 1 0 1 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 0 1 0
0 0 1 1 1 1 1 1
0 1 0 1 1 1 1 0
0 1 0 1 1 1 1 0
0 0 0 1 1 1 1 0
2repositioning
input array:
{0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
output array:
{0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 }
Implement it using two pointer. First pointer points to the 0 in odd position and second one points to the 1 in even position. In the loop, swap the items in the positions where the pointers pointed to and find the next suitable positions for pointers.
3. Binary numbers |
--> the immediate lower number with same number of ones
* if all digits are '1', it does not exist;
* at least one digit is '0' :
swap the leftmost '0' with the '1' at its left and
rearrange the digits at its right '1's first and '0's then.
--> the next higher number with same number of ones
* swap the rightmost '1' with the '0' at its left and
rearrange the digits at its right '0's first and '1's then.
(if all digits are '1', the '0' at its left is in front of the number, not written)
4. Microsoft question |
5. Binary Format
Given a number, how would you check to see if it can be accurately represented in binary (eg can 76.2782783 be represented accurately in binary
6. Reverse Linked List
... {
if(item.next!=null)
...{
Item tmp=reverseLinkedList(item.next);
item.next.next=item;
item.next=null;
return tmp;
}
return item;
}
... {
Item previous=null;
while(item!=null)
...{
Item tmp=item.next;
item->next=previous;
previous=item;
item=tmp;
}
return item;
}
7. Codejam question
Alex has a lot of contacts in his Hyper Instant Messenger (HIM), and therefore, it takes some time to find the right contact. HIM supports Contact Groups, and each contact can be placed into a group. However, HIM's group support is rather limited. It allows for only one of the following two scenarios: 1) Multiple groups, but every contact belongs to a single group, or 2) All contacts exist only in a global contacts list (and no groups are created).
It takes Alex k seconds to find an entry in any list of k entries. If groups exist, HIM will first show a list of all the groups, and once a group is selected, a list of all contacts within that group will be displayed. If groups do not exist, HIM will simply show a single list containing all the contacts in the global list. For example, if Alex has 5 contacts in the global list, it will take 5 seconds to find any one of them. If Alex splits those contacts into two groups with 2 and 3 users each, it will first take him 2 seconds to find the right group, and then 2 or 3 seconds to find the right contact depending on which group he chooses. You are given an int N representing the total number of contacts. Arrange the contacts optimally (either in one global contacts list or in several groups) such that the maximal time needed to find a contact is minimized. Return this time and the groups formed.
8. Finding least common ancestor of two nodes
... {
if(node==null)
return null;
if(node.left==a || node.right==a || node.left==b || node.right==b)
return node;
Node p=findLCA(a, b, node->left);
Node q=findLCA(a, b, node->right);
if(p!=null && q!=null)
return node;
else return (p!=null)?p:q;
}
It is not important which node is returned. It is important that the return value is whether null or not.
9. Google Q
We are given an array a1...an and we need to construct b1...bn where bi = a1*a2*...*an/ai.
We are allowed only constant space and the time complexity is O(n). No divisions are allowed.
10. Binary search on circular array.
How can we search a number in O(lgn) in a circularly sorted array?
Original array-1,5,7,11,16,20
Circularly sorted array-16,20,1,5,7,11.
We don't know the position of the pivot around which the array has been rotataed.
11. array split
Given an array wtih length greater than 2.
U need to split the array into 2 such that the average of the two arrays must b same.
test case:
{2,3,4} is the input
xpectd o/p: {3} {2,4}
average is 3 in both the cases.
Note: If there is no solution then the system shud halt displayin "IMPOS".
12. Microsoft question
Given an long string, design an algorithm to find the maximum
repetitive substring. For example, the maximum repetitive substring of "abcabcabc" is "abcabc"
1 prefix-tree
2 Build a matrix and find the longest bias
13. rotate array using constant spance and o(n) time.
You are given an array with n elements { x1, x2 ... xk ... xn }. You are also given an array location k.
Using constant space and O(n) time, rotate the array such that it contains { xk, xk+1 ... xn, x1, x2 ... xk-1 }.
It is like reverse the words in a statement with 2 words
14. Searching an element in rotated sorted array?
An element in a sorted array can be found in O(log n) time via binary search. But suppose I rotate the sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Now devise a way to find an element in the rotated array in O(log n) time.
... {
if(left==right)
...{
if(array[left]==key)
return array[left];
else
return -1;
}
int middle=(right+left)/2;
if(array[left]<array[middle] && array[left]<=key && key<=array[middle])
return rotatedBinarySearch(array, left, middle) ;
else if(array[left]>array[middle] && (key>array[left] || key<array[middle])
return rotatedBinarySearch(array, left, middle);
else if(array[middle+1]<array[right] && array[middle+1]<=key && array[right]>=key)
return rotatedBinarySearch(array, middle+1, right);
else if(array[middle+1]>array[right] && (key>array[middle=1] || key<array[right])
return rotatedBinarySearch(array, middle+1, right);
return -1;
}
15. Pretty iterative permutations
Write iterative algo to print these permutations.
let N equal the length of a []
create an integer array p [] of size N+ 1 to control the iteration
initialize p [ 0 ] to 0 , p [ 1 ] to 1 , p [ 2 ] to 2 , ... , p [ N ] to N
initialize index variable i to 1
while (i < N) do {
decrement p [ i ] by 1
if i is odd , then let j = p [ i ] otherwise let j = 0
swap(a [ j ] , a [ i ] )
let i = 1
while (p [ i ] is equal to 0 ) do {
let p [ i ] = i
increment i by 1
} // end while (p [ i ] is equal to 0 )
} // end while (i < N)