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)?
- A. Right to left, keeping the running maximum of values seen so far.
- B. Left to right, keeping the running minimum.
- C. Random order using a hash set.
- 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.
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
- In the asteroid collision problem, why does a stack of right-moving asteroids suffice?
- 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…