This question evaluates a candidate's ability to design and analyze a data structure that supports stack operations with efficient maximum retrieval and removal, testing algorithmic design, complexity analysis, and handling of edge cases such as duplicates and empty-structure behavior.
Design a stack that supports push (x), pop(), top(), peekMax(), and popMax(). The popMax operation must remove and return the maximum element; if there are multiple maximums, remove the most recently pushed one. Implement two approaches: (A) an amortized O(