Implement stacks, streaming median, and upward path sum
Problem Statement
This interview consists of several coding problems. For each problem, you are required to implement the specified functionality, state the time and space complexity of your solution, and identify key edge cases.
1. MinStack
Design a stack data structure that supports the following operations:
push(x): Pushes elementxonto the stack.pop(): Removes the element at the top of the stack.top(): Returns the element at the top of the stack.getMin(): Returns the current minimum value in the stack.
All operations, including getMin(), must run in O(1) amortized time.
2. MaxStack
Design a stack data structure that supports the following operations:
push(x): Pushes elementxonto the stack.pop(): Removes the element at the top of the stack.top(): Returns the element at the top of the stack.peekMax(): Returns the current maximum value in the stack.popMax(): Removes and returns the current maximum value from the stack. If there are duplicate maximum values, the one closest to the top of the stack should be removed.
Describe your chosen data structure and its complexity for each operation.
3. Streaming Median from a Data Stream
Design a data structure that can efficiently receive numbers from a continuous stream and supports the following operations:
addNum(x): Adds an integerxfrom the data stream into the data structure.findMedian(): Returns the median of all elements seen so far.
Target efficiency: addNum should run in O(log n) time, and findMedian should run in O(1) time.
4. How to Modify MaxStack to Support Real-Time Median
Building upon your MaxStack implementation, discuss how you would adapt or extend that data structure to efficiently support stack-like operations (push, pop, top, peekMax, popMax) while also supporting findMedian() in real-time. Describe any additional data structures needed, the complexity of each operation, and how consistency would be maintained across these combined functionalities.
5. Binary Tree: Upward-Only Path Sum
Given the root of a binary tree and an integer targetSum, determine whether there exists a path such that:
- The path can start from any node in the tree.
- Each step in the path must move from the current node to its parent (i.e., the path direction is strictly upward).
- The sum of node values along this path equals
targetSum.
Your output should be a boolean (true/false).
Note: If the node structure does not inherently include a parent pointer, assume the input only provides the root node, and you must handle the "upward" constraint during your traversal or by augmenting the tree structure.
AI interview prep
Core dilemma
The central engineering conflict is maintaining multiple order statistics (min, max, median) across dynamic data structures with strict O(1) or O(log N) time complexity constraints for various operations, especially when elements can be removed from arbitrary positions (e.g., `popMax`) or when combining functionalities.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement stacks, streaming median, and upward path sum
Problem Statement
This interview consists of several coding problems. For each problem, you are required to implement the specified functionality, state the time and space complexity of your solution, and identify key edge cases.
1. MinStack
Design a stack data structure that supports the following operations:
push(x): Pushes elementxonto the stack.pop(): Removes the element at the top of the stack.top(): Returns the element at the top of the stack.getMin(): Returns the current minimum value in the stack.
All operations, including getMin(), must run in O(1) amortized time.
2. MaxStack
Design a stack data structure that supports the following operations:
push(x): Pushes elementxonto the stack.pop(): Removes the element at the top of the stack.top(): Returns the element at the top of the stack.peekMax(): Returns the current maximum value in the stack.popMax(): Removes and returns the current maximum value from the stack. If there are duplicate maximum values, the one closest to the top of the stack should be removed.
Describe your chosen data structure and its complexity for each operation.
3. Streaming Median from a Data Stream
Design a data structure that can efficiently receive numbers from a continuous stream and supports the following operations:
addNum(x): Adds an integerxfrom the data stream into the data structure.findMedian(): Returns the median of all elements seen so far.
Target efficiency: addNum should run in O(log n) time, and findMedian should run in O(1) time.
4. How to Modify MaxStack to Support Real-Time Median
Building upon your MaxStack implementation, discuss how you would adapt or extend that data structure to efficiently support stack-like operations (push, pop, top, peekMax, popMax) while also supporting findMedian() in real-time. Describe any additional data structures needed, the complexity of each operation, and how consistency would be maintained across these combined functionalities.
5. Binary Tree: Upward-Only Path Sum
Given the root of a binary tree and an integer targetSum, determine whether there exists a path such that:
- The path can start from any node in the tree.
- Each step in the path must move from the current node to its parent (i.e., the path direction is strictly upward).
- The sum of node values along this path equals
targetSum.
Your output should be a boolean (true/false).
Note: If the node structure does not inherently include a parent pointer, assume the input only provides the root node, and you must handle the "upward" constraint during your traversal or by augmenting the tree structure.