Data Structures & AlgorithmsHard

In the 'remove nodes with a greater value to their right' problem, which list order is processed to decide survivors in O(n)?

  1. A. Right to left, keeping the running maximum of values seen so far.
  2. B. Left to right, keeping the running minimum.
  3. C. Random order using a hash set.
  4. D. Left to right, deleting any node that has a smaller neighbour.

Correct answer: A. Right to left, keeping the running maximum of values seen so far.

A node survives only if its value is at least the maximum of everything after it, so scanning from the right with a running maximum decides each node in O(1). Left-to-right scans cannot see the future values.

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

Exams that ask this topic