Data Structures & AlgorithmsModerateWipro · Capgemini pattern
Starting from an empty stack: push 5, push 3, pop, push 8, push 2, pop, pop, push 7, push 1. What are the stack contents from top to bottom?
- A. 1, 7, 8, 5
- B. 5, 7, 1
- C. 1, 7, 2, 5
- D. 1, 7, 5
Correct answer: D. 1, 7, 5
After push 5, 3 and pop: [5]. Push 8, 2 → [5, 8, 2]; two pops remove 2 then 8 → [5]. Push 7, 1 → [5, 7, 1]; top first: 1, 7, 5. '5, 7, 1' lists bottom to top.
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
- A circular queue uses an array of size 5 (indices 0–4); front is the index of the first element and rear is the index of the last element…
- Double hashing in a table of size 11 uses h1(k) = k mod 11 and h2(k) = 7 − (k mod 7); probe i goes to (h1 + i·h2) mod 11. Keys 14, 25, 36…
- How many DISTINCT palindromic substrings (counted as strings, not by position) does 'abaab' have?
- For each element of [4, 10, 5, 8, 20, 15, 3, 12], find the nearest SMALLER element to its LEFT (−1 if none) using a monotonic stack. What…
- The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time. Which of these pop sequences is…
- Top-down merge sort (split until single elements, then merge) sorts an array of 13 elements. How many times is the merge step called?