Stock Price Fluctuation

You are receiving a continuous stream of stock price records containing a timestamp and a price. Because of network volatility, records can arrive out of order, and some might even contain incorrect prices that get corrected by a later record with the exact same timestamp.

Design a StockPrice class to:

  • StockPrice(): Initializes the object.
  • void update(int timestamp, int price): Updates the price at a specific timestamp, replacing any previous price recorded at that exact time.
  • int current(): Returns the latest price (the price associated with the highest recorded timestamp).
  • int maximum(): Returns the absolute highest price recorded so far across all timestamps.
  • int minimum(): Returns the absolute lowest price recorded so far across all timestamps.

(Note: The output will strictly print the integers returned by current, maximum, and minimum, ignoring the update and constructor commands.)

Example 1:

Input: commands = ["StockPrice", "update", "current"] args = [[], [1, 10], []]

Output: [10]

Explanation:

We record a price of 10 at timestamp 1. Calling current() returns 10 since it is the only recorded time.

Example 2:

Input: commands = ["StockPrice", "update", "update", "maximum"] args = [[], [2, 20], [2, 5], []]

Output: [5]

Explanation:

The price at time 2 is initially recorded as 20, but then immediately corrected to 5. Calling maximum() correctly returns 5.

Now Your Turn!

Pick the correct output for the given input

Input: commands = ["StockPrice", "update", "update", "minimum", "current"]args = [[], [5, 10], [2, 30], [], []]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= timestamp, price <= 109
  • At most 105 calls will be made in total to update, current, maximum, and minimum.
  • current, maximum, and minimum will be called only after update has been called at least once.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class StockPrice {
public:
StockPrice() {
}
void update(int timestamp, int price) {
}
int current() {
}
int maximum() {
}
int minimum() {
}
};
 
/**
* Your StockPrice object will be instantiated and called as such:
* StockPrice* obj = new StockPrice();
* obj->update(timestamp,price);
* int param_2 = obj->current();
* int param_3 = obj->maximum();
* int param_4 = obj->minimum();
*/
Test Case

Input:

Args
Commands