Data Structures & AlgorithmsModerateGATE CS · DSA
A queue is implemented with two stacks (push to IN; pop from OUT, refilling OUT from IN only when OUT is empty). Which statement is correct?
- A. The amortised cost per operation is Θ(log n)
- B. A single dequeue can cost Θ(n), but any sequence of n operations costs O(n) in total
- C. Every dequeue costs O(1) in the worst case
- D. Every enqueue costs Θ(n)
Correct answer: B. A single dequeue can cost Θ(n), but any sequence of n operations costs O(n) in total
Each element is pushed onto IN once, moved to OUT once and popped once — at most 3 stack operations per element — so the total is linear even though one dequeue may move many elements.
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
- ^ is exponentiation (highest precedence, right-associative); and / come next; + and − are lowest; others are left-associative. What is the…
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…
- Only the lower triangle (including the diagonal) of a 12 × 12 matrix is stored row by row in a 1-D array B starting at B[0] (indices…
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…
- You need to sort 10⁷ integers, each in the range 0 to 999. Which approach is asymptotically fastest?
- Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed? python a, k = [8, 7, 7, 10, 5, 11…