Question
What will be the output of the following queue implementation using two stacks? class QueueUsingStacks { Stack s1 = new Stack <>(); Stack s2 = new Stack <>(); void enqueue ( int
What will be the output of the following queue implementation using two stacks? class QueueUsingStacks { Stack s1 = new Stack <>(); Stack s2 = new Stack <>(); void enqueue ( int
x) { s1.push(x); } int dequeue () { if (s2.isEmpty()) { while (!s1.isEmpty()) { s2.push(s1.pop()); } } if (!s2.isEmpty()) { return s2.pop(); } throw new RuntimeException ( "Queue is empty!" ); } } QueueUsingStacks queue = new QueueUsingStacks (); queue.enqueue( 1 ); queue.enqueue( 2 ); queue.enqueue( 3 ); System.out.println(queue.dequeue()); queue.enqueue( 4 ); System.out.println(queue.dequeue());
More Data Structure Questions
- Which of the following is true about triggers in a relational database?
- A hash table using separate chaining has 10 slots and stores 15 elements, uniformly distributed across the slots. What is the average number of comparisons...
- In an adjacency matrix, checking whether edge (u,v) exists takes:
- Which statement correctly explains why Dijkstra's algorithm produces incorrect shortest-path results on graphs containing negative edge weights, even when ...
- Which of the following is an example of an emerging technology that is most likely to impact the future of computing?
- What type of relationship is represented by Aggregation in Object-Oriented Programming?
- Which cryptographic concept ensures that data integrity is preserved and cannot be altered during transmission?
- Using Kruskal's algorithm on a graph with edges A-B(1), B-C(3), A-C(4), C-D(2), B-D(5), what is the total weight of the Minimum Spanning Tree?
- Which OSI model layer is responsible for reliable delivery of data between devices?
- What is the purpose of the keys() method in a dictionary?
Hey! Ask a query
Please enter email id
The email must be a valid email address.
Please enter Mobile Number
Please enter valid Mobile Number
Please enter your Doubt
Think You're Ready for RBI Grade B?
RBI Grade B 2026 Phase 1 Memory Based Paper
- 200 Questions with Detailed Solutions
- Section-wise Coverage (GA, English, Quant & Reasoning)