Data Structure Multiple Choice Questions on “Stack using Queues”.
1. To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?
a) 1
b) 2
c) 3
d) 4
Answer: b
Clarification: Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.
2. Making the push operation costly, select the code snippet which implements the same.(let q1 and q2 be two queues)
a)
public void push(int x) { if(empty()) { q1.offer(x); } else{ if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q2.offer(q1.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int size = q2.size(); while(size>0) { q1.offer(q2.poll()); size--; } } } }
b)
public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q1.offer(x); int size = q1.size(); while(size>0) { q2.offer(q1.poll()); size--; } } else if(q2.size()>0) { q2.offer(x); int size = q2.size(); while(size>0) { q1.offer(q2.poll()); size--; } } } }
c)
public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q1.offer(q2.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int size = q2.size(); while(size>0) { q2.offer(q1.poll()); size--; } } } }
d)
public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q2.offer(q2.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int size = q2.size(); while(size>0) { q2.offer(q1.poll()); size--; } } } }
View Answer
Answer: a
Clarification: Stack follows LIFO principle, hence a new item added must be the first one to exit, but queue follows FIFO principle, so when a new item is entered into the queue, it will be at the rear end of the queue. If the queue is initially empty, then just add the new element, otherwise add the new element to the second queue and dequeue all the elements from the second queue and enqueue it to the first one, in this way, the new element added will be always in front of the queue. Since two queues are needed to realize this push operation, it is considered to be costlier.
3. Making the push operation costly, select the code snippet which implements the pop operation.
a)
public void pop() { if(q1.size()>0) { q2.poll(); } else if(q2.size()>0) { q1.poll(); } }
b)
public void pop() { if(q1.size()>0) { q1.poll(); } else if(q2.size()>0) { q2.poll(); } }
c)
public void pop() { q1.poll(); q2.poll(); }
d)
public void pop() { if(q2.size()>0) { q1.poll(); } else { q2.poll(); } }
View Answer
Answer: b
Clarification: As the push operation is costly, it is evident that the required item is in the front of the queue, so just dequeue the element from the queue.
4. Select the code snippet which returns the top of the stack.
a)
public int top() { if(q1.size()>0) { return q1.poll(); } else if(q2.size()>0) { return q2.poll(); } return 0; }
b)
public int top() { if(q1.size()==0) { return q1.peek(); } else if(q2.size()==0) { return q2.peek(); } return 0; }
c)
public int top() { if(q1.size()>0) { return q1.peek(); } else if(q2.size()>0) { return q2.peek(); } return 0; }
d)
public int top() { if(q1.size()>0) { return q2.peek(); } else if(q2.size()>0) { return q1.peek(); } return 0; }
View Answer
Answer: c
Clarification: Assuming its a push costly implementation, the top of the stack will be in the front end of the queue, note that peek() just returns the front element, while poll() removes the front element from the queue.
5. Select the code snippet which return true if the stack is empty, false otherwise.
a)
public boolean empty() { return q2.isEmpty(); }
b)
public boolean empty() { return q1.isEmpty() || q2.isEmpty(); }
c)
public boolean empty() { return q1.isEmpty(); }
d)
public boolean empty() { return q1.isEmpty() & q2.isEmpty(); }
View Answer
Answer: b
Clarification: If both the queues are empty, then the stack also is empty.
6. Making the pop operation costly, select the code snippet which implements the same.
a)
public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>0) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q2.size(); while(count>0) q1.offer(q2.poll()); res = q2.poll(); } return res; }
b)
public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>1) q2.offer(q1.poll()); res = q2.poll(); } if(q2.size()>0) { count = q2.size(); while(count>1) q1.offer(q2.poll()); res = q1.poll(); } return res; }
c)
public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>1) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q2.size(); while(count>1) q1.offer(q2.poll()); res = q2.poll(); } return res; }
d)
public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q2.size(); while(count>1) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q1.size(); while(count>1) q1.offer(q2.poll()); res = q2.poll(); } return res; }
View Answer
Answer: c
Clarification: Here the pop operation is costly, hence we need two queues, other than the first element, all the the elements from one queue are dequeued and enqueued to the second queue, hence only one element remains in the first queue which is the item we want, so dequeue it and return the result.
7. What is the functionality of the following piece of code?
public void fun(int x) { q1.offer(x); }
a) Perform push() with push as the costlier operation
b) Perform push() with pop as the costlier operation
c) Perform pop() with push as the costlier operation
d) Perform pop() with pop as the costlier operation
Answer: b
Clarification: offer() suggests that it is a push operation, but we see that it is performed with only one queue, hence the pop operation is costlier.