Connection Status:
Competition Arena > StockQuotes
SRM 257 · 2005-08-08 · by lars2520 · Simple Math
Class Name: StockQuotes
Return Type: String[]
Method Name: report
Arg Types: (vector<string>)
Problem Statement

Problem Statement

A 'quote' is a message from a stock exchange that says at what price that exchange is currently willing to buy a given stock (the 'bid') and what price it is willing to sell the stock (the 'ask'). The 'spread' is defined as the difference between the ask and the bid. For our purposes, you can assume that the ask will always be greater than the bid and that the bid will be greater than zero. The 'inside quote' is defined as the highest bid from any exchange and the lowest ask from any exchange (they do not necessarily need to be the same exchange). The inside quote changes when any exchange improves upon the current inside quote or when the best exchange moves away from the inside exposing a lower bid or higher ask.

You will be given a String[] representing a number of quotes, in the order they are made. Each element of the input will represent a single quote and will be formatted as: "EXCHANGE BID ASK", where EXCHANGE is the index of the stock exchange making the quote and BID and ASK are as defined above. You are to issue a report based on the quotes. Each element of the report should be formatted as "EXCHANGE COUNT AVERAGE_SPREAD", where EXCHANGE is either the index of an EXCHANGE, as in the input, or 10, representing the inside quote. COUNT is the number of times that the quote (either bid or ask) for that exchange changed (or that the inside quote changed). AVERAGE_SPREAD is the average spread, averaged only over the quotes that caused the quote to change and with exactly 2 digits after the decimal point (rounded in the standard way to the nearest hundredth where .005 rounds up). The return should contain one entry for each exchange that issues one or more quotes in the input. It should be sorted by the index of the exchanges, with the entry for the inside spread coming last.

Constraints

  • Each element of quotes will be formatted as "EXCHANGE BID ASK" where EXCHANGE is a digit between '0' and '9', inclusive, and BID and ASK are integers between 1 and 1000, inclusive, without leading zeros.
  • quotes will contain between 1 and 50 elements inclusive.
  • An exchange will never issue a quote that is the same is its existing quote (so the number of times that its quote changes will be identical to the number of quotes it issues).
  • ASK will be greater than BID in each element of quote.
  • The inside spread will always be greater than 0.
Examples
0)
{"0 10 14",
 "1 9 16",
 "2 11 15",
 "0 11 13",
 "1 10 15",
 "2 12 14",
 "0 9 15",
 "2 8 20"}
Returns: {"0 3 4.00", "1 2 6.00", "2 3 6.00", "10 6 2.83" }

The following table shows how the best bid and ask change as new bids come in: | BEST Exch|Bid |Ask | Bid|Ask | Changed ----+----+----+----+----+------ 0 | 10 | 14 | 10 | 14 | Yes 1 | 9 | 16 | 10 | 14 | 2 | 11 | 15 | 11 | 14 | Yes 0 | 11 | 13 | 11 | 13 | Yes 1 | 10 | 15 | 11 | 13 | 2 | 12 | 14 | 12 | 13 | Yes 0 | 9 | 15 | 12 | 14 | Yes 2 | 8 | 20 | 10 | 15 | Yes

1)
{"1 68 72","1 65 71","0 64 77","2 65 76","1 65 77","0 66 77","5 64 77","8 67 75","1 67 74","1 64 77"}
Returns: {"0 2 12.00", "1 5 8.40", "2 1 11.00", "5 1 13.00", "8 1 8.00", "10 7 7.71" }
2)
{"8 931 944",
 "8 926 946",
 "8 926 951",
 "8 928 953",
 "8 929 954"}
Returns: {"8 5 21.60", "10 5 21.60" }
3)
{"6 796 804","0 796 802","8 796 811","7 791 809","7 796 805","9 793 805","5 793 807","8 794 803","1 787 805","5 786 802","8 790 807","6 793 803","3 792 802","6 790 802","6 789 800","8 796 805","1 788 802","8 790 801","8 791 801","8 790 803","7 792 804","2 790 801","9 790 804","2 791 802","0 786 803","1 786 799","5 783 801","2 792 800","8 787 804","1 792 806","7 790 800","6 791 802","2 791 806"}
Returns: {"0 2 11.50", "1 4 14.75", "2 4 11.25", "3 1 10.00", "5 3 16.00", "6 5 10.40", "7 4 12.25", "8 8 12.63", "9 2 13.00", "10 6 6.83" }
4)
{"0 639 648",
 "9 640 651",
 "5 638 650",
 "9 637 651",
 "6 637 652",
 "8 638 653",
 "7 637 651",
 "6 635 652",
 "3 636 653",
 "1 634 653",
 "0 635 654",
 "4 635 654",
 "3 634 652",
 "0 635 655",
 "5 635 653"}
Returns: {"0 3 16.00", "1 1 19.00", "3 2 17.50", "4 1 19.00", "5 2 15.00", "6 2 16.00", "7 1 14.00", "8 1 15.00", "9 2 12.50", "10 5 10.20" }

Submissions are judged against all 65 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class StockQuotes with a public method vector<string> report(vector<string> quotes) · 65 test cases · 2 s / 256 MB per case

Submitting as anonymous