Question: Where code is wrong You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of

Where code is wrongYou are given a stream of records about a particular stock. Each

You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp. Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record. Design an algorithm that: Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp. Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded. Finds the maximum price the stock has been based on the current records. Finds the minimum price the stock has been based on the current records. Implement the StockPrice class: StockPrice () Initializes the object with no price records. void update(int timestamp, int price) Updates the price of the stock at the given timestamp. . int current() Returns the latest price of the stock. int maximum () Returns the maximum price of the stock. int minimum() Returns the minimum price of the stock. 1 2 3 4 59 6 7 8Y 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24- 25 26 27 28 29 30 31 32 33 Y class StockPrice { public: }; map mp; multiset ms; StockPrice() { } void update(int timestamp, int price) { if(mp.find(timestamp)!=mp.end()) { } auto it=ms.find(mp[timestamp]); ms.erase(it); } mp[timestamp]=price; ms.insert(price); } int current() { auto it=mp.end(); it--; return it->second; } int maximum() { auto it=ms.end(); return *it; } int minimum() { auto it=ms.begin(); return *it;

Step by Step Solution

3.45 Rating (155 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

ANSWER To solve this problem we can use a hash table to keep track of the latest price at each timestamp Whenever we receive a new record we can updat... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Computer Network Questions!