Data Structures & AlgorithmsHard

Asteroids in a row have sizes given by 5 8 -3 -8 10 -4 2. Positive values move right, negative values move left; two meeting asteroids collide, the smaller explodes (equal sizes both explode), and a survivor keeps its direction. What remains, from left to right?

  1. A. 5 8 -3 -8 10 -4 2
  2. B. 2 10 5
  3. C. 5 10
  4. D. 5 10 2

Correct answer: D. 5 10 2

Process asteroids left to right with a stack of the ones moving right; a left-moving asteroid pops smaller right-movers, and equal sizes destroy both. A left-moving asteroid that survives with no right-mover left in the stack is simply pushed.

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

Exams that ask this topic