GATE Computer ScienceVery hardGATE CS (PYQ pattern)
A queue is built from two stacks: enqueue pushes onto stack IN; dequeue pops from OUT, and if OUT is empty it first moves every element of IN to OUT one by one (a move is one pop and one push). Counting every push and every pop as one primitive operation, how many primitives does the sequence E E E D E E D D E E D D D perform (E = enqueue, D = dequeue)?
- A. 27
- B. 25
- C. 30
- D. 33
Correct answer: A. 27
Each enqueue costs 1 (push). Each dequeue costs 1 (pop) plus 2 per element moved at refill. Here 7 enqueues cost 7, 6 dequeues cost 6, and two refills (of 3 and 4 elements) add 2 per moved element = 14; total 27.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A BST is built by inserting 50, 30, 70, 20, 40, 60, 80 in some order into an initially empty tree. The final tree is the perfectly…
- Insert 45, 25, 65, 15, 35, 55, 75, 10, 20, 30, 40, 50, 60 into an empty BST. Then delete 25, 45 and 65 in that order, replacing a node…
- A full binary tree (every node has 0 or 2 children) has preorder 1 2 4 5 8 9 3 6 10 11 7 and postorder 4 8 9 5 2 10 11 6 7 3 1. What are…
- Binary trees (unlabelled, left and right children distinguished) with exactly 6 nodes: how many have height exactly 3 (a single node has…
- Height is counted in edges (a single node has height 0). What is the maximum possible height of an AVL tree with 50 nodes, and what is the…
- How many distinct binary trees (left and right children are distinguished) on nodes 1..5 have preorder 1 2 3 4 5 and postorder 5 4 3 2 1?