ProgrammingModerate

A queue is needed for 10^5 operations on an array. Which dequeue is correct and fast enough?

  1. A. Shift every element left with memmove each time, O(n) per dequeue
  2. B. Keep a head index and increment it, O(1) per dequeue
  3. C. Call qsort on the array before each dequeue
  4. D. Scan from the start to find the oldest element, O(n) per dequeue

Correct answer: B. Keep a head index and increment it, O(1) per dequeue

A head index makes dequeue O(1), and the cost stays linear overall. Shifting the array makes the total about n^2 / 2 element moves.

More C Programming questions

Exams that ask this topic