StreakPeaked· Practice

ExamsGATETechnical

A queue is implemented using a non-circular singly linked list. The queue has a head pointer and a tail pointer, as shown in the figure. Let n denote the number of nodes in the queue. Let enqueue be implemented by inserting a new node at the head, and dequeue be implemented by deletion of a node from the tail. Which one of the following is the time complexity of the most time-efficient implementation of enqueue and dequeue, respectively, for this data structure?

  1. Θ(1), Θ(1)
  2. Θ(1), Θ(n)
  3. Θ(n), Θ(1)
  4. Θ(n), Θ(n)

Correct answer: Θ(1), Θ(n)

Solution

In this implementation, enqueueing a new node at the head can be done in constant time, Θ(1), since it only requires adjusting the head pointer. However, dequeuing from the tail requires traversing the entire list to find the second-to-last node, resulting in a linear time complexity of Θ(n).

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →