GATE Computer ScienceHardGATE CS (PYQ pattern)
A queue is implemented by two stacks (enqueue pushes onto IN; dequeue pops from OUT, first moving all elements of IN to OUT if OUT is empty). The operations in order are E E E D E E D D E D E E E D D D E E (E = enqueue, D = dequeue). How many individual element moves from IN to OUT take place?
- A. 7
- B. 11
- C. 16
- D. 9
Correct answer: D. 9
OUT is empty at operations 4, 10 and 16 (the 1st, 4th and 7th dequeues), and each time IN holds 3 elements, so 3 + 3 + 3 = 9 moves occur. The two elements enqueued last never leave IN, so the answer is 9, not 11.
More Algorithms questions
- Keys 15, 9, 12, 4, 18, 7, 3, 10, 21, 6 are inserted one by one into an empty binary min-heap (array representation, standard sift-up)…
- Floyd's Build-Heap performs at most height(v) exchanges while sifting down node v, so its exchanges are bounded by H = Σ height(v) over…
- A 3-ary min-heap stores 1000 keys in an array A[0..999], with the children of A[i] at 3i + 1, 3i + 2 and 3i + 3. How many of the keys are…
- Computing x¹²⁷ by multiplication, where each step multiplies two powers already computed (squarings count as multiplications), what is the…
- Euclid's algorithm gcd(a, b) repeatedly replaces (a, b) by (b, a mod b) until b = 0, counting one step per mod operation. Over all pairs 1…
- The Sieve of Eratosthenes on 2..100 crosses out the multiples of each prime p ≤ 10, starting from p² (p², p² + p, …). How many numbers are…