Stack and Queue

Stack:A stack is a linear list of elements for which all insertions and deletions(usually accesses) are made at only one end of the list.

They are also called as LIFO lists(Last Input First Output).
The operations supported are :

1)IsEmpty(S): returns whether the stack is empty or not.

2)IsFull(S): return whether the stack is full or not.

3)Push(Element X,S): pushes element X on to the top of the stack.

4)Pop(S) : pops an element from the top of the stack on to the output(printing on to the output console isn't necessary though in which case we can define another function Top(S) which gives the top element of the stack).

All the above mentioned operations are of O(1) complexity.

Queue: Queue is a linear list for which all insertions are made at one end and deletions(accesses as well)
are made at the other end of the list and the lists are also called as FIFO lists(First Input First Output ).

The operations supported are

1)IsEmpty(Q):returns whether the queue is empty or not.

2)IsFull(Q): return whether the queue is full or not.

3)Enqueue(Element X,Q): inserts an element X on the rear side of the queue.

4)Dequeue(Q): removes the element pointed to by the front end of the queue.

Similar to a stack ,the operations of the queue are also of O(1) complexity.

Dequeue (double ended queue):A Dequeue is a linear list for which insertions and deletions(accesses as well) occur at the ends.
Analogous to the operations defined for stack and Queue,we can also define some operations for Dequeue.
A simple observation reveals the fact that we can simulate both stack and queue from Dequeue by input and output restrictions.

Having dwelt at such a length on these linear lists,we shall also see some interesting questions based on the simple properties of these linear lists.


1)How do you implement 2 stacks using only one array.Your stack routines should not indicate an overflow unless every slot in the array is used?

Solution:given an Array,start the first stack S1 from left end and other stack S2 from the right end.while S1 gets grows towards right ,S2 grows towards left.

2)Propose a data structure which supports the stack Push and Pop operations and a third operation FindMin,which returns the smallest element in the data strucuture all in O(1) worst case time.
Solution:Use 2 stacks S1 in to which the elements are pushed and S2 in to which only the current minimum is pushed.
When one needs to insert an element E ,we first push E on to S1 and then access the top element T of S2 which is the minimum before E has been inserted.If only E is less than T , we push E on to S2 .
When one needs to pop an element ,pop the top element of S1 and if this element is also equal to the one on top of S2, then pop it off S2 as well.

Hence the current minimum will always be on top of S2 .Hence along with other normal stack operations, access of minimum element is also possible in O(1).

3)Show how to implement 3 stacks in a single array efficiently?(debated question)

Solution: It is still up for debate ,as we haven't yet figured out the exact solution.We will soon put the best solution for this problem.

4)Consider a empty stack of integers.Let the numbers 1,2,3,4,5,6 be pushed on to this stack only in the order they appeared from left to right.Let S indicates a push and X indicate a pop operation.Can they be permuted in to the order 325641(output) and order 154623?(if a permutation is possible give the order string of operations.
(Hint: SSSSSSXXXXXX outputs 654321)

Solution: SSSXXSSXSXXX outputs 325641.
154623 cannot be output as 2 is pushed much before 3 so can appear only after 3 is output.

5)Given a string containing N S's and N X's where S indicates a push operation and X indicates a pop operation, and with the stack initially empty,Formulate a rule to check whether a given string S of operations is admissible or not (Hint: A string S of operations should always abide by the properties of the stack which in this case only means you never pop an element from an empty stack)

Solution:Given a string of length 2N, we wish to check whether the given string of operations is permissible or not with respect to its functioning on a stack.
The only restricted operation is pop whose prior requirement is that the stack should not be empty.So while traversing the string from left to right,prior to any pop the stack shouldn't be empty which means the no of S's is always greater than or equal to that of X's.
Hence the condition is at any stage on processing of the string,
no of S's > no of X's

6)Find a simple formula for An, the number of permutations the can be printed on an input of n distinct characters to a stack (similar to question 4)

Solution:The numbers are input in the order 1,2,3,...,N.
So the problem amounts to the number of strings each of N pushes(denoted by S) and N pops(denoted by X).
The only criteria to select a string is at any stage of its processing character by character we should have no of S's > no of X's .

This problem is no different from parenthesis problem where N needs to give the no of possible permutations of N parenthesis , which if given by Nth catalan number Cn=(2n)!/((n+1)! n!).
For more insite into catalan number refer this link.

7)Show that it is possible to obtain the permutation P1P2.........Pn from 1,2,.........n using a stack
if and only if there are no indices i < j < k such that Pj < Pk < Pi.

Solution:The solution can be arrived simply by veirfying that among the 6 possible
orders of Pi,Pj,Pk the rest 5 are possible and the ordering in question i.e is Pi,Pj,Pk is not possible.

We leave it to the reader the verification of possibility of the 5 orderings and deal only with proving that the order Pi,Pj,Pk is not possible.

Suppose say that the order Pi,Pj,Pk is possible.
As Pi is the largest and printed first (i < j < k) followed by Pj and Pk,just before the popping of Pi the ordering of these 3 on the stack shall be Pi,Pk followed by Pj(from top).But as j<k ,Pj is printed prior to Pk contradicting the ordering on the stack. Hence this ordering is not possible.

Please Post Your answers to these Questions in the comments section.Your valuable comment might invoke a good discussion and learning process.

More questions to follow on this topic soon!!


  1. For Qn 7) could you tell me what the other 5 possibilities are there?

  2. there are 6 possible orderings possible for these 3 elements ..

    I hope it clears ur doubt..

  3. In Q4 for creating a permutation of order 154623 use the following -

    Any pair of SX will move the top element back in the order. So first SSXX moves 1, 2 after 6 as 2, 1. Then push 3 which we want as first element in the final order. I suppose rest should be trivial.

  4. hello brother tell me some good site for latest C interview or important questions like your blog your blog is very good for questions .....
    my ID is ""

  5. Q3: you can implement 3 stacks in an array by choosing the mth element to be starting point for 2 & 3rd stacks. so now the 2nd array will grow towards 1st element, and 3rd array will grow towards last element.

  6. soory friend i was testing only how to send

  7. abhishek khare,jnuMarch 11, 2011 5:53 PM

    good and basic questions.....................

  8. answers are not up to answer of question-5 contradicts ur question.plz explain properly the answer of question-6.plz take it as a positive criticism..thank for all question

  9. Hi

    Tks very much for post:

    I like it and hope that you continue posting.

    Let me show other source that may be good for community.

    Source: Architect interview questions

    Best rgs

  10. I am very impressed with the thorough explanations. Very nicely written. Thank you. This helps me a lot.

  11. Hi

    I like this post:

    You create good material for community.

    Please keep posting.

    Let me introduce other material that may be good for net community.

    Source: Amazon interview questions

    Best rgs

  12. Hi There,

    Thanks for highlighting this and indicating about Freshers Interviews where more study and thought is necessary.

    If I buy a Route 53 domain name, can I create an email address and use it with gmail? If so, how?
    I do not want to use Workmail, or SES or an EC2 instance.
    All I want is a username, password, and SMTP and whatever else is needed to link my route 53 email to gmail.

    Very useful article, if I run into challenges along the way, I will share them here.


  13. Hey Brother,

    Such vivid info on the Freshers Interviews Flabbergasted! Thank you for making the read a smooth sail!

    I have just received an email that says, "We're writing to provide you with an electronic invoice for your use of AWS services for the billing period April 1 - April 30, 2018. AWS Training Additional information regarding your bill, individual service charge details, and your account history are available on the Billing & Cost Management Page."

    Thanks a lot. This was a perfect step-by-step guide. Don’t think it could have been done better.

    Thank you,
    Irene Hynes

  14. Marhaba,

    Great piece on

    Technical & HR Interview Questions of Google,Microsoft,Yahoo and many more Companies, I’m a fan of the ‘flowery’ style Looking forward to more long form articles ??

    I have AWS ec2 six instance for 3 year subscription and type is c4.xlarge for purpose of web-server. due to high usage of RAM/CPU/IO, we wanted extend capacity means resizes resources for existing c4.xlarge to c42xlarge type. may I know the how much extra cost we need to pay for each instance. if we want resize ec2 instances. AWS Training USA

    Super likes !!! for this amazing post. I thinks everyone should bookmark this.

    Thanks and Regards

  15. Thanks for sharing it.I got Very valuable information from your blog.your post is really very Informative. I’m satisfied with the information that you provide for me.

    Best Mean Stack Training in Pune

  16. Good Post! it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article, I want to say that also a well-written article with some very good information which is very useful for the AWS Cloud Practitioner Online Training

  17. Hey, thanks for the blog article.Really looking forward to read more. Cool.
    . net online training
    dot net online training hydarabad