Adobe Interview Question
Software Engineer / DevelopersO(N) algorithm here it goes
Rotate(int beg, int mid, int last)
/*
beg = 0
mid = index of ele. which is to be made fst ele
last = No. of ele. {12, 22 , 35} last = 3
*/
{
int next = mid;
while(beg != next)
{
swap(arr[beg], arr[next]);
beg++; next++
if(next == last) next = mid;
else if(beg == mid) mid = next;
}
}
This is the algorithm used in C++ STL Rotate function. I hope interviewer wont expect better than this.
The above mentioned algo will not work.
@saumitra, Can you make it work for 123456789 and i want to change it in 678912345.
Yes
123456789
Reverse : 987654321
Reverse first 4 chars: 678954321
Reverse last 5 chars : 678912345
I think saumitra's algo( used in C++ STL ) will work in that case too i.e., when 'next' crosses the last element, the array will be like this
678951234 but 'beg' is pointing to 5 and 'next' is set to mid which is pointing to 1 (the index where 6 was there originally) the while condition still holds true and the elements after swap will be 678915234 at this point since beg==mid mid is set to next and the rest of elements will be swapped the same way and when next becomes the last the array will be 678912345
hey come on man..please refrain from using Hindi..not every here understands Hindi and it really comes across as stupid
- ABC May 21, 2010