In: Computer Science
Suppose an initially empty stack S has performed a total of 15 push operations, 12 top operations, and 13 pop operations ( 3 of which returned null to indicate an empty stack ). What is the current size of S?
Question 1 options:
Question 2 (1 point)
Saved
What values are returned during the following series of stack operations, if executed upon an initially empty stack? push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop().
seperate by commas ie: 1, 2, 3, 4, 5, 6, 7, 8, 9
Question 2 options:
Question 3 (1 point)
Saved
What values are returned during the following sequence of queue operations, if executed on an initially empty queue? enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9), enqueue(1), dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(), enqueue(4), dequeue(), dequeue().
seperate by commas ie: 1, 2, 3, 4, 5, 6, 7, 8, 9
Question 3 options:
Question 4 (1 point)
Saved
What values are returned during the following sequence of deque ( double ended queue ) ADT operations, on an initially empty deque? addFirst(3), addLast(8), addLast(9), addFirst(1), last( ), isEmpty( ), addFirst(2), removeLast( ), addLast(7), first( ), last( ), addLast(4), size( ), removeFirst( ), removeFirst( ).
seperate by commas ie: 1, 2, 3, 4, 5, 6, 7, 8, 9
Question 4 options:
Question 5 (1 point)
Saved
Suppose an initially empty queue Q has performed a total of 30 enqueue operations, 10 first operations, and 15 dequeue operations, 5 of which returned null to indicate an empty queue. What is the current size of Q?
Question 5 options:
Question1 Answer:
A push operation inserts an item into the stack and a pop deletes an item from the stack. A top operation doesn't delete any item but just tells the value of top element in stack.
Now, 15 push, 12 top and 13 pop operations are performed. These are not necessarily sequential but can be in mixed order. For example, first we push 3 elements and then pop 4 elements. On the 4th pop, null will be returned to indicate empty stack.
In total, out of 13 pop operations, 3 are wasted as null is returned. So, at the end, there will be 15 - (13 - 3) elements in stack i.e 5 elements
Question2 answer:
push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7), push(6), pop(), pop(), push(4), pop(), pop().
The returned values are 3,8,2,1,6,7,4,9
Question 3 answer:
enqueue(5), enqueue(3), dequeue(), enqueue(2), enqueue(8), dequeue(), dequeue(), enqueue(9), enqueue(1), dequeue(), enqueue(7), enqueue(6), dequeue(), dequeue(), enqueue(4), dequeue(), dequeue().
The returned values are 5,3,2,8,9,1,7,6
Question 5 Answer:
An enqueue operation inserts an item into the queue and a dequeue operation deletes an item from the queue. A first operation doesn't delete any item but just tells the value of first element in the queue.
Now, 30 enqueue operations, 10 first operations, and 15 dequeue operations are performed. These are not necessarily performed sequentially but can be in mixed order. For example, first we enqueue 3 elements and then dequeue 4 elements. On the 4th dequeue, null will be returned to indicate empty queue.
In total, out of 15 dequeue operations, 5 are wasted as null is returned. So, at the end, there will be 30 -(15 - 5) elements in the queue i.e 20 elements in the queue.