recategorized by
4,006 views

2 Answers

Best answer
5 votes
5 votes

A.

Operation Push(1) Push(2) Push(3) Pop Pop Pop Push(4) Push(5) Pop Pop
      3              
    2 2 2       5    
  1 1 1 1 1   4 4 4  
          Output       3 32 321 321 321 3215 32154

C.

Operation Push(1) Push(2) Push(3) Pop Push(4) Pop Push(5) Pop Pop Pop
      3   4   5      
    2 2 2 2 2 2 2    
  1 1 1 1 1 1 1 1 1  
          Output       3 3 34 34 345 3452 34521

 

Both Option(A) and (C) satisfied here.

selected by
2 votes
2 votes
A and C are valid...

Just insert the elements into the stack as per given order...here order is given 1,2,3,4,5...

So if input contains any order which doesn't follow this is invalid...

 

For D..

insert 1,2,3 into the stack now top symbol is similar to the  input symbol just pop it...

After popping 3 top of the stack is 2 not matched with remaining first input symbol...

So insert 4 into stack..Now matched pop it...

Same way insert 5 and pop it...

Now the I input symbol is 1 but top of the stack is 2...so this sequence is not followed

For B...

1,2,3 ,4,5  we need to insert but our stack size is 3 so not possible...

For A..

Push 1,2,3 now top matches so pop 3,2,1...

Push 4,5 top matches with input so pop 5,4

For C..

push 1,2,3, top match with input 3 so pop it...

Now push 4 , top matches with input so pop 4

Now push 5, top matches with input so pop 5,2,1
edited by
Answer:

Related questions

0 votes
0 votes
2 answers
1
go_editor asked Jul 11, 2016
4,938 views
Given an empty stack, after performing push(1), push(2), pop, push(3), push(4), pop, pop, push(5), pop, what is the value of the top of the stack?4321
1 votes
1 votes
2 answers
2
go_editor asked Jul 7, 2016
10,064 views
The following postfix expression is evaluated using a stack. 823^/23*+51*- . The top two elements of the stack after first * is evaluated6,15, 73,21, 5
1 votes
1 votes
1 answer
4
go_editor asked Jul 13, 2016
3,003 views
A* algorithm is guaranteed to find an optimal solution ifh’ is always 0g is always 1h’ never overestimates hh’ never underestimates h