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?
- A. A circular buffer (ring array) with head index, when its capacity is not exceeded
- B. A singly linked list with head and tail pointers
- C. A doubly linked list
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the length of the shortest contiguous subarray of [1, 3, 5, 6, 3, 3, 7, 9, 4, 9] whose sum is at least 20? (All values are…
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [1…
- Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [5, 42, 29, 4, 41, 8, 53…
- Stock prices over 8 days are [27, 32, 42, 42, 35, 66, 59, 31]. The span of day i is the number of consecutive days ending at day i…
- Assertion (A): Binary search runs in O(log n) time on a sorted singly linked list. Reason (R): Binary search halves the search range in…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…