Design stack with O(1) minimum query
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of stack-based data structures, constant-time operation constraints, and handling edge cases such as empty queries, duplicate entries, and negative values.
Constraints
- 1 <= len(operations) <= 2 * 10^5
- For 'push', -10^9 <= value <= 10^9
- Each operation must run in O(1) time
- pop on an empty stack should do nothing
Examples
Input: ([('push', 5), ('push', 2), ('push', 2), ('min', None), ('pop', None), ('min', None), ('top', None)],)
Expected Output: [2, 2, 2]
Explanation: After pushing 5,2,2: min=2. Pop removes top 2, min remains 2, top is 2.
Input: ([],)
Expected Output: []
Explanation: No operations produce output.
Input: ([('min', None), ('top', None), ('pop', None), ('push', -1), ('min', None), ('top', None)],)
Expected Output: [None, None, -1, -1]
Explanation: min/top on empty return None. pop on empty does nothing. After push -1, min/top are -1.
Input: ([('push', 3), ('push', 1), ('push', 2), ('min', None), ('pop', None), ('min', None), ('pop', None), ('min', None), ('top', None)],)