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...
-
The chapter says HP has more $goodwill than the $book value of the firm. Explain what this means, how it could occur, and what can be done about this situation?
-
Caldwell was shopping in a K-Mart store, carrying a large purse. A security guard observed her look at various small items such as stain, hinges, and antenna wire. On occasion she bent down out of...
-
After a series of celebrated accounting debacles, Congress enacted the Sarbanes-Oxley Act of 2002, which introduced tighter regulation of the accounting industry under a new Public Company Accounting...
-
R.D. Manufacturing Inc.'s joint cost of producing 1,000 units of Product A, 500 units of Product B, and 500 units of Product C is $20,000. The unit sales values of the three products at the split-off...
-
Exercise 1 Motion on a Ramp In this exercise you will roll a marble down a ramp and calculate its linear acceleration and velocity. You will then create graphs to illustrate the motion of the marble.
-
Assume that the Siletz River data are lognormally distributed. Find the following. (a) Peak flow of the 100-yr flood (b) Peak flow of the 50-yr flood (c) Probability that a flood will be less than or...
-
On 5/1/19, the Albers Company borrowed $33,000 from Johnson Bank on a 1- year, 6% note payable. Interest was not deducted in advance. If the company's fiscal year ends on 6/30/19, an entry is needed...
-
Let the volatility \(\sigma\) be nonconstant and a function of the underlying asset, \(\sigma(A(t))\). Use the chain rule to compute the smile-adjusted delta of a call option.
-
In what ways do you think the psychological contract is threatened by the shortcomings in the implementation of performance management at Stiles et al.s organisations?
-
XYZ companys annual sales for its products is 5000 units and its average inventory is 500 units. a. Compute the companys inventory turns. b. If XYZ decides to increase its inventory turns, how will...
-
Susan was a one-third partner in Anders Mountain Partners until last month. Last month, she received a cash distribution of \(\$ 25,000\) in "redemption" (i.e., partial liquidation) of half her...
-
What other potential problems may be relevant to the introduction or implementation of performance management?
-
Discuss the concepts of hypothesis testing, including what you are evaluating, when it should be used, and the differences between a one- and a two-tailed test. Describe 1 example from your own...
-
You continue to work in the corporate office for a nationwide convenience store franchise that operates nearly 10,000 stores. The per- store daily customer count (i.e., the mean number of customers...
-
A running mountain lion can make a leap 10.0 m long, reaching a maximum height of 3.0 m. a. What is the speed of the mountain lion just as it leaves the ground? b. At what angle does it leave the...
-
In the 2016 Olympics in Rio, after the 50 m freestyle competition, a problem with the pool was found. In lane 1 there was a gentle 1.2 cm/s current flowing in the direction that the swimmers were...
-
A soccer player takes a free kick from a spot that is 20 m from the goal. The ball leaves his foot at an angle of 32, and it eventually hits the crossbar of the goal, which is 2.4 m from the ground....
Study smarter with the SolutionInn App