The GATE Grind

GATE 2024 DA – Question 32

Programming, Data Structures and Algorithms · Stacks, queues, linked lists, trees and hash tables · 1 mark · Numerical answer

The fundamental operations in a double-ended queue D are:
insertFirst(e) – Insert a new element e at the beginning of D.
insertLast(e) – Insert a new element e at the end of D.
removeFirst() – Remove and return the first element of D.
removeLast() – Remove and return the last element of D.

In an empty double-ended queue, the following operations are performed:

insertFirst(10)
insertLast(32)
a <- removeFirst()
insertLast(28)
insertLast(17)
a <- removeFirst()
a <- removeLast()

The value of a is ______.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 17

Explanation

After the first two operations D is [10, 32]. removeFirst gives 10 and D is [32]. After the two insertLast operations D is [32, 28, 17]. The next removeFirst gives 32 and D is [28, 17]. The final removeLast returns 17, so $a = 17$.