Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which of the following operations on a queue data structure has a time complexity of O(1) ?

(a) Enqueue (b) Dequeue (c) Peek (d) Clear

  • A.a and b
  • B.b only
  • C.c only
  • D.a, b and c

Answer: D

The O(1) operations are a, b and c: enqueue, dequeue and peek.

With front and rear pointers (in a linked list or circular array), enqueue adds at the rear, dequeue removes at the front and peek reads the front, each in constant time whatever the length of the queue.

Clear has to release or reset every element, so it generally takes time proportional to the number of elements. That rules out options leaving out enqueue or peek.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.