DriveReady

P08 — Stacks and queues — every solution explained

This lesson is part of full access

One payment of ₹250 opens every lesson, practice set and mock for all five companies. No subscription, no expiry.

What is inside

  • 1. Reverse a string using a stack
  • 2. Balanced brackets across three kinds
  • 3. Next strictly greater element to the right; -1 where there is none
  • 4. Next strictly smaller element to the right; -1 where there is none
  • 5. Previous strictly greater element to the left; -1 where there is none
  • 6. Stock span: for each day, how many consecutive days back the price was <= today's
  • 7. Evaluate a space-separated postfix expression over integers
  • 8. Repeatedly remove adjacent duplicate characters until none remain
  • 9. Do two strings match after applying '#' as a backspace?
  • 10. Does the expression contain a redundant bracket pair, e.g. ((a+b)) or (a)?
  • 11. Largest rectangle in a histogram, via a monotonic stack. O(n)
  • 12. First non-repeating character in a stream, after each character arrives. '#' where every character seen so far repeats
  • 13. A queue built from two stacks. Amortised O(1) per operation
  • 14. A stack that also reports its minimum in O(1)