Data Structures & AlgorithmsHardProduct-company MCQ pattern
A queue uses two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving ALL of IN into OUT only if OUT is empty. For enq 1, enq 2, enq 3, deq, enq 4, deq, enq 5, deq, deq, how many single-element moves from IN to OUT happen in total?
- A. 7
- B. 4
- C. 3
- D. 5
Correct answer: D. 5
The first deq moves 3 elements (1, 2, 3) and returns 1; the next two deqs find OUT non-empty and return 2, 3; the fourth deq finds OUT empty and moves 4 and 5, then returns 4. Total 3 + 2 = 5. Moving on every dequeue would give more.
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 postfix form of (A + B) C − (D − E) (F + G)?
- What is the value of the prefix expression − + 2 3 4 / 8 2 ?
- Infix A (B + C (D − E)) / F is converted to postfix with an operator stack (operators and '(' are pushed onto it). What is the largest…
- Using a sliding window, find the length of the longest substring with no repeated character in the string dvdfabcda.
- What is the length of the shortest contiguous subarray of [1, 4, 4, 2, 3, 1, 5] whose sum is at least 9?
- How many contiguous subarrays of [1, 2, 3, −3, 3, 1] have sum exactly 3? (A prefix-sum + hash-map method counts them in O(n).)