Simple one but little tricky.
Implement a function something like RotateArray(int array[], int nLen, int rotateBy), that will rotate the array by the given ammount.
For ex array is 1,2,3,4,5,6,7,8 and RotateArray(array, 8, 3) (rotateBy is 3) so afterthe function call the arry sould look like: 6,7,8, 1,2,3,4,5
..
Ok here goes my solution( O(n) ):
void RotateArray(int arr[], int len, int rotateBy) {
int i = 0;
int j = 0;
rotateBy = rotateBy%len;
do {
for (i = 0;i < rotateBy; i++) {
EXCHANGE(arr[j+i], arr[len-rotateBy+i])
}
j += i;
} while(j < len-rotateBy);
}
Friday, March 14, 2008
Classic 2 egg problem!
Picked up from http://classic-puzzles.blogspot.com/2006/12/google-interview-puzzle-2-egg-problem.html
Problem statement you can get on the above link. Now task is to generalize it for N story building and E eggs.
Problem statement you can get on the above link. Now task is to generalize it for N story building and E eggs.
Find the mid point of a link list and delete a node(given the pointer to this node) from a very long circuilar link list
The are just to warm you up..
First one is to find the middle element in the link list!
Hint: think about 2 pointers.
Second one is , delete a node P(given is the pointer to P) without traversing list to get to the previous node.
Hint: you shouldn't need one :)
First one is to find the middle element in the link list!
Hint: think about 2 pointers.
Second one is , delete a node P(given is the pointer to P) without traversing list to get to the previous node.
Hint: you shouldn't need one :)
Thursday, March 6, 2008
Convert a tree into a liked list where nodes in the linked list represent level order traversal.
Now a simpler one:
For ex. if tree is
.............|1|..............
............./..\..............
.........|2|.... |3|..........
....... / \...... / \..........
.... |4|..|5| ..|6|...|7|....
Make a list like 1-->2-->3-->4-->5-->6-->7.
For ex. if tree is
.............|1|..............
............./..\..............
.........|2|.... |3|..........
....... / \...... / \..........
.... |4|..|5| ..|6|...|7|....
Make a list like 1-->2-->3-->4-->5-->6-->7.
Subscribe to:
Posts (Atom)