Amazon None Interview Question
NonesCountry: India
Interview Type: Written Test
I think for enqueue also we might have to reverse the stack, if the last operation was dequeue, since we are using the same stack.
So it would be better to keep track of the last operation performed using a flag. fLastOperation = enqueu/dequeue.
Agreed. Something like an isReversed would help - that way, if you have to dequeue multiple items in sequence, you can do each one in O(1) time.
For Enqueue:
- kartheek October 13, 2012Only 1 Operation
{Push}
For Dequeue:
3 Opreations we require
{reverse,pop.reverse}