• edited by
9,819 views
31 31 votes

A program attempts to generate as many permutations as possible of the string, '$abcd$' by pushing the characters $a, b, c, d$ in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?

  1. $abcd$
  2. $dcba$
  3. $cbad$
  4. $cabd$

3 Answers

Best answer
44 44 votes
  1. push $a$ & pop $a$, push $b$ & pop $b$, push $c$ & pop $c$, and finally push $d$ and pop $d$
    sequence of popped elements will come to $abcd$
  2. first push $abcd$, and after that pop one by one, sequence of popped elements will come to $dcba$
  3. push $abc$, and after that pop one by one sequence of popped elements will come to $cba$, now push $d$ and pop $d$, final sequence comes to $cbad$
  4. this sequence is not possible because '$a$' can not be popped before '$b$' any how
• edited by
2 2 votes

Option A :- abcd => ()()()()

Option B :- dcba => (((())))

Option C :- cbad => ((()))()

Option D :- cabd => Not Possible

Therefore, Correct Option is D) cabd

Answer:
Position:
Show:

Related questions

68 68 votes
5 answers 5 answers
17.8k
17.8k views
Ishrat Jahan asked Nov 2, 2014
17,831 views
An array of integers of size $n$ can be converted into a heap by adjusting the heaps rooted at each internal node of the complete binary tree starting at the node $\left ...
84 84 votes
9 answers 9 answers
40.7k
40.7k views
Ishrat Jahan asked Nov 1, 2014
40,704 views
Let $P$ be a singly linked list. Let $Q$ be the pointer to an intermediate node $x$ in the list. What is the worst-case time complexity of the best-known algorithm to del...
31 31 votes
5 answers 5 answers
8.0k
8.0k views
Ishrat Jahan asked Nov 2, 2014
8,013 views
Which one of the following binary trees has its inorder and preorder traversals as $BCAD$ and $ABCD$, respectively?
3 3 votes
1 answers 1 answer
3.9k
3.9k views
Ishrat Jahan asked Nov 2, 2014
3,909 views
Given below are several usages of the anchor tag in HTML.<A HREF = "http://www.gate.ac.in/HTML/BASIC/testpage.html">Test Me</A><A HREF = "/BASIC/testpage.html">Test Me</A...