Data Structures & AlgorithmsVery hard
In the asteroid collision problem, why does a stack of right-moving asteroids suffice?
- A. Only the most recent right-mover can ever collide with the current left-mover, so the stack top is the only candidate.
- B. All right-movers collide with every left-mover at once.
- C. Left-movers never collide with anything.
- D. Stacks sort asteroids by size automatically.
Correct answer: A. Only the most recent right-mover can ever collide with the current left-mover, so the stack top is the only candidate.
A left-moving asteroid meets the nearest surviving right-mover first, which is the top of the stack. Once that one is handled, the next candidate becomes the new top, so a stack models the process exactly.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A linear-time check decides whether a permutation can be sorted by one stack. Which property is equivalent to being stack-sortable?
- In the jump game (minimum number of jumps), why does a single left-to-right scan with a 'current boundary' count the jumps correctly?
- In the 'first non-repeating character in a stream' problem, why is a queue with lazy removal used?
- Why is a hash map (not a sorted array) used to find the longest run of consecutive integers in an unsorted array in linear expected time?
- When tracking the first occurrence of every value for an order-preserving de-duplication, which structure gives expected O(1) membership…
- A subarray's average is at least K exactly when which quantity is non-negative for that subarray?