Re: Converting a LIFO into FIFO
- From: "Alf P. Steinbach" <alfps@xxxxxxxx>
- Date: Thu, 12 Nov 2009 11:49:48 +0100
* arnuld:
Sometime ago, some friend of mine faced an interview for a programming position and he was asked this question:
----------------------------------------------------
Suppose you have two LIFOs: A and B. The only operations you can do on A and B are push() and pop(), These LIFOs have these properties:
1) Elements are always added into A (never into B) and are added in ascending order: 1,2,3,.... N.
2) You pop() an element from A and push() that into B.
3) During pop() from A and push() into B, 1 or more elements may or may not be pushed() into A.
4) The only elements added into B are the ones which are pop()ed from A.
Final question is how you can develop an algorithm so that whenever you pop() an element from B its in the same sequence as it was added. I mean when you push()ed elements into A in ascending order: 1,2,3,...N, hence when you pop from B, the first element should be 1, 2nd pop() should be 2 and so on..
-------------------------------------------------------
That seems like too much of theoretical question to me. Any ideas ?
It's just a problem due to a bunch of constraints.
If any of those constrains can be removed (doing anything else than the described situation) then there is no problem.
So the question is only what constraint(s) you are "allowed" to remove for your solution.
Sounds like a trick question.
"No, you can't do /that/".
Cheers & hth.,
- Alf
.
- Follow-Ups:
- Re: Converting a LIFO into FIFO
- From: mohangupta13
- Re: Converting a LIFO into FIFO
- From: H. J. Sander Bruggink
- Re: Converting a LIFO into FIFO
- References:
- Converting a LIFO into FIFO
- From: arnuld
- Converting a LIFO into FIFO
- Prev by Date: Converting a LIFO into FIFO
- Next by Date: Re: Converting a LIFO into FIFO
- Previous by thread: Converting a LIFO into FIFO
- Next by thread: Re: Converting a LIFO into FIFO
- Index(es):
Relevant Pages
|