Max Stack
Updated
Problem statement
Implement a max stack by processing the finite array operations from left to right. The stack starts empty.
Each operation has one of these forms:
PUSH value: pushvalueonto the top of the stack.POP: remove and return the top value.PEEK: return the top value without removing it.PEEK_MAX: return the largest value currently in the stack without removing it.POP_MAX: remove and return the largest value. If the maximum occurs more than once, remove the occurrence closest to the top.
Return the values produced by every operation except PUSH, in command order.
Function
runMaxStack(operations: String[]) → int[]Examples
Example 1
operations = ["PUSH 5","PUSH 1","PUSH 5","PEEK","POP_MAX","PEEK","PEEK_MAX","POP","PEEK"]return = [5,5,1,5,1,5]POP_MAX removes the upper copy of 5. The stack is then [5,1] from bottom to top.
Example 2
operations = ["PUSH 2","PUSH 2","PUSH 1","POP_MAX","PEEK","POP","PEEK_MAX"]return = [2,1,1,2]The topmost maximum is the second pushed 2, even though 1 is above it. Removing that node leaves 1 on top.
Constraints
1 <= operations.length <= 100000.- Every operation is exactly one documented command;
PUSHhas one separating space before its value. -10^9 <= value <= 10^9.- Every
POP,PEEK,PEEK_MAX, andPOP_MAXis issued while the stack is nonempty. - At least one operation produces output.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Max StackLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Max StackZipline · ONSITE INTERVIEW
- Stack with Middle OperationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Min StackCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- LFU CacheAmazon · ONSITE INTERVIEW
- LFU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Least Frequently Used CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Recover a Function-Call LRU Cache with a Write-Ahead LogAnthropic · ONSITE INTERVIEWAnthropic interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)