391 views
0 votes
0 votes
We want to reverse elements of stack using a queue.Suppose stack operations,push and pop take 1 unit of time each and queue operations insert and delete take 2 units of time each.Assume n elements are there in the stack.The minimum time to reverse the stack is

(A) 4n units

(B) 6n units

(C) 8n units

(D) 9n units

1 Answer

3 votes
3 votes
(B) 6n units is the answer.

Lets take an example the stack is having n elements present in it. Now to reverse them using queue we have to perform two steps n times.

1. pop the element from the stack and insert it to the queue . This will take n+2n time units respectively.

2.delete the element from the queue and push it into stack.This will take 2n+n time units respectively.

so the total time = 3n+3n=6n

Related questions

9 votes
9 votes
5 answers
2
target2017 asked Jan 7, 2017
2,968 views
How it is 24? I'm not getting it.
0 votes
0 votes
1 answer
3
deepak_8404 asked Oct 1, 2023
280 views
Consider a lower triangular matrix stored in row major order as p[-25 - - + 749][-25 - - - + 749] with base address = 6800, size of each element = 6 byte. Find the value ...
0 votes
0 votes
1 answer
4
jugnu1337 asked May 16, 2023
804 views
The total number of binary trees possible with height n - 2 having n nodes are?(2n - 5)^ 2n - 3 (2n - 7)^2n - 3(n - 3) ^2n - 2(2n - 7)^ 2n - 2