Where code is wrong You are given a stream of records about a particular stock. Each record
Fantastic news! We've Found the answer you've been seeking!
Question:
Where code is wrong
Transcribed Image Text:
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<int, int>mp; multiset<int>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; 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<int, int>mp; multiset<int>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;
Expert Answer:
Answer rating: 100% (QA)
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 the full answer
Posted Date:
Students also viewed these computer network questions
-
Is it wrong for marketers to create wants where none exist in the marketplace in order to make profits? Support your answer.
-
What is wrong with the following code fragment? Will this code compile if it is part of an otherwise valid program? Explain.
-
Where must a companys code of ethics be made available?
-
After researching Best Buy common stock, Sally Jackson is convinced the stock is overpriced. She contacts her account executive and arranges to sell short 200 shares of Best Buy. At the time of the...
-
Describe data and process modeling concepts and tools.
-
The cost of a border fence is $3 million per mile. If the life of such a fence is assumed to be 10 years, the equivalent annual cost of a 10-mile-long fence at an interest rate of 10% per year is...
-
An MNCs cash flows and therefore its valuation can be affected by expected exchange rate movements (as explained in Chapter 1). Sanoma Co. is a U.S.-based MNC that wants to assess how its valuation...
-
Bert founded Sambert Corporation a little over a year ago. He believes that his company, which sells specialized computer toys, will be very profitable over the next several years, as evidenced by...
-
K Which of the accompanying normal quantile plots appear to represent data from a population having a normal distribution? Explain. Click the icon to view the normal quantile plots a. Does this...
-
Open MTL_ QRTStudy. pdf, which documents the work of a quality response team at MyTVLab. Read the internal report that documents the work of the team and their conclusions. Then answer the following:...
-
A severe hurricane watch has boon issued in your area for the next two days. There are pook days of business. This will be the first time in your new space that a warning such as this has been...
-
In________________ a experiment, subjects do not decide for themselves which treatment they will get. In Exercises 510, fill in each blank with the appropriate word or phrase.
-
Is on-demand work a viable option for you? Explain.
-
Fill in the Blank. The Laplace transform method converts a differential equation into a(n)__________equation.
-
A poll conducted by the American Research Group asked individuals their views on how the economy will be a year from now. Respondents were given four choices: Better than today, Same as today, Worse...
-
In a _________________study, the subjects are followed over time. In Exercises 510, fill in each blank with the appropriate word or phrase.
-
Many cost functions are Output levels. in nature when considered over the entire range of possible a. Choppy. b. Nonexistent. c. Linear. d. Flat. e. Nonlinear.
-
Critical reading SAT scores are distributed as N(500, 100). a. Find the SAT score at the 75th percentile. b. Find the SAT score at the 25th percentile. c. Find the interquartile range for SAT scores....
-
White Weddings has to decide whether to accept a contract to run a wedding. They want to make 2,000 profit and the fixed costs are 4,000. The contribution per guest (sale) after variable costs is 25....
-
How can CVP analysis aid business planning?
-
Below are details of a number of costs, please categorise them into the various categories (fixed, variable, semi-variable): Food served at an event as part of the attendance fee per person,...
Study smarter with the SolutionInn App