Data Structures & AlgorithmsHardGATE CS
A queue is built from two stacks: an enqueue pushes onto the input stack; a dequeue pops from the output stack, and if the output stack is empty it first moves every element from the input stack to the output stack (one pop and one push per element). Counting every single push and pop on the two stacks, how many are performed by E1 E2 E3 D E4 D D E5 E6 D E7 D D D E8 D (E = enqueue, D = dequeue)?
- A. 32
- B. 24
- C. 29
- D. 35
Correct answer: A. 32
Enqueues cost 1 each (8 total), dequeues cost 1 each (8 total), and each of the 8 element transfers costs 2 more: 8 + 8 + 2 x 8 = 32. Forgetting the double cost of a transfer gives 24.
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
- From the digit string 1432219836475, exactly 5 digits are deleted (the remaining digits keep their order, and leading zeros are not an…
- An array next = [10, 6, 10, 1, 1, 9, 5, 5, 10, 3, 7] defines a linked structure in which the node after index i is next[i]. Floyd's…
- Two sorted lists A = [2, 4, 4, 7, 9, 12, 15, 18, 21] and B = [1, 4, 5, 6, 13, 14, 16, 17] are merged with the standard merge procedure…
- Insertion sort (stable, scanning leftwards while a[j] > key) sorts [6, 3, 9, 3, 1, 8, 2, 7, 5, 4]. Counting every evaluation of the…
- For each index i of [4, 7, 3, 8, 2, 2, 9, 1, 6, 5, 10, 3], let g(i) be the index of the first element to the right that is strictly…
- An array of 15 elements is rotated right by 6 positions using three in-place reversals (reverse the whole array, then reverse the first 6…