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
- 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 data structure follows the First-In-First-Out (FIFO) principle, where the first element added is the first one to be removed?
- What is the time complexity of inserting a new element at the beginning (head) of a singly linked list?
- Which OOP principle allows for restricting access to certain parts of an object while exposing only necessary parts for interaction?
- In the context of system analysis, which of the following best defines the "use case" diagram?
- If a program crashes with a "NullPointerException" (or similar null reference error), what is the most likely cause?
- A data analysis algorithm requires processing data in a Last-In, First-Out (LIFO) manner. Which operation is used to remove the most recently added element...
- What is the maximum number of edges possible in a simple directed graph (no self-loops, no parallel edges) containing 6 vertices, where an edge from A to B...
- Which of the following scenarios violates Boyce-Codd Normal Form (BCNF) in a relational schema?
- For Dijkstra’s algorithm on a graph with non-negative weights, which data structure yields the best time complexity for dense graphs?
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)