Data Structures & AlgorithmsVery hardProduct company OA

Adjacent equal pairs are removed repeatedly with a stack until none remain. What is left of 'pqqprrsttsx'?

  1. A. ssx
  2. B. px
  3. C. x
  4. D. (empty)

Correct answer: C. x

Stack trace: p, q, q pops q → [p]; p pops p → []; r, r cancel; s; t, t cancel → [s]; s cancels s → []; x → [x]. Removing only the pairs visible in the ORIGINAL string (qq, rr, tt) and stopping leaves 'ppssx' → 'ssx' after one more round – the stack does all rounds in one pass.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic