Data Structures · Question 15 · Hard

Data Structures question 15

Two stacks share a single array arr[0..n-1]. Stack 1 starts at index 0 and grows right (top1 starts at -1); stack 2 starts at index n-1 and grows left (top2 starts at n). When does a push to either stack overflow?

  1. When top1 == n / 2
  2. When top1 + 1 == top2
  3. When top2 == 0
  4. When top1 == top2
Answer and explanation

Answer: B. When top1 + 1 == top2

Explanation: The stacks grow toward each other and can use any free slot. The array is full only when no gap is left between them, i.e. top1 + 1 == top2. Splitting at n / 2 would waste space.

Previous: How many structurally different binary trees can be formed with exactly 3 nodes? · Next: Which data structure is the most natural choice for implementing the Undo…

Practise data structures

Try a fresh set of data structures questions with an explanation after every answer, free and without signing up: Data Structures practice. Or sit a full placement mock test.

More data structures questions

More technical & programming practice

Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge