IAE Academy

GATE 2024 DA Q.32: solution and answer

GATE 2024 · DAQ.32 · 1 mark · NAT

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 ______.

See the question in the paper

Answer and solution

ANSWER

The value of a is 17.

Official answer: 17Source: Official answer key, GATE 2024 DA (IISc Bengaluru)

  1. Start with the empty deque: D = [].

  2. insertFirst(10) puts 10 at the front, so D = [10].

  3. insertLast(32) appends at the back, so D = [10, 32].

  4. a ← removeFirst() removes and returns the front element, so a = 10 and D = [32].

  5. insertLast(28) gives D = [32, 28], and insertLast(17) gives D = [32, 28, 17].

  6. a ← removeFirst() removes the front element, so a = 32 and D = [28, 17].

  7. a ← removeLast() removes the back element, so a = 17 and D = [28].

  8. Each assignment overwrites a, so the final value of a is the one returned by the last operation, which is 17.

KEY IDEA

Trace the deque operation by operation. a is overwritten at every assignment, so only the last removal (removeLast()) decides the final value.

WATCH OUT FOR

Reporting the value from the first or second removal (10 or 32), or treating insertFirst as an append at the back. Another slip is to forget that a is reassigned, so only the last removal counts.

From the GATE 2024 DA paper. Every question of this paper · Download the full paper · All 13 Linear structures questions, with notes

More Linear structures questions