Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Which data structure gives O(1) worst-case time for inserting and deleting at BOTH ends and also O(1) access by index?

  1. A. A circular buffer (ring array) with head index, when its capacity is not exceeded
  2. B. A singly linked list with head and tail pointers
  3. C. A doubly linked list
  4. D. A stack implemented with an array

Correct answer: A. A circular buffer (ring array) with head index, when its capacity is not exceeded

A ring array supports push/pop at both ends by moving head/tail indices modulo capacity and computes the i-th element as (head + i) mod capacity. Linked lists need O(n) for index access; a singly linked list also cannot delete its tail in O(1).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic