Data Structures & AlgorithmsVery hardGoogle · Stacks

A greedy stack algorithm removes exactly 3 digits from the number string "1432219" to get the smallest possible number (keeping the order of the remaining digits). What is the result?

  1. A. 1422
  2. B. 1219
  3. C. 1229
  4. D. 2219

Correct answer: B. 1219

Scan left to right and pop the stack top while it is larger than the current digit and removals remain: 4, 3 and one 2 are removed, leaving 1219.

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

Exams that ask this topic