Implement system to get most traded stocks in C#












0












$begingroup$


Please suggest way to solve a coding question, I came across. Question is as follows:



Implement a class StockMarket that has the two following functions:



 class StockMarket
{

public:
void addTrade(string stockName, int share);
- This function keeps track the number of shares transacted for a stock by adding the trade information
void printTop(int numberOfStock);
- This function prints the top numberOfStock stocks that has the highest number of transacted shares

}


And here's a sample calling sequence that uses the above class:



 StockMarket stockMarket = new StockMarket();
stockMarket.addTrade("GOOGLE", 50);
stockMarket.addTrade("APPLE", 150);
stockMarket.addTrade("GOOGLE", 100);
stockMarket.addTrade("MSFT", 250);
stockMarket.addTrade("GOOGLE", 200);

stockMarket.printTop(2);


Expected output:
GOOGLE 350
MSFT 250



I followed 2 approaches to solve this problem:




  1. Create Dictionary with Key - CompanyName and value - number of shares. While giving top results, I sorted dictionary by descending and returned results. This



  2. Create SortedSet with custom comparer to sort stocks with descending order. I am facing challanges to update SortedSet. I am not able to remove old entries with time less than O(n). Please find below my current code :



    public class StockContainer
    {
    public Dictionary map;
    public SortedSet sortedStockList;



    public StockContainer()
    {
    map = new Dictionary<string, StockNode>();
    sortedStockList = new SortedSet<StockNode>(new StockNodeComparer());
    }

    public void AddValue(string s, int x)
    {
    if(map.ContainsKey(s))
    {
    int tempVal = map[s].value;
    sortedStockList.Remove(map[s]);
    var tempNode = new StockNode(s, tempVal + x);
    AddNodeInDict(s, ref tempNode);
    sortedStockList.Add(tempNode);
    }
    else
    {
    var tempNode = new StockNode(s, x);
    AddNodeInDict(s, ref tempNode);
    sortedStockList.Add(tempNode);
    }
    }

    private void AddNodeInDict(string s, ref StockNode tempNode)
    {
    map[s] = tempNode;
    }

    public void GetTop(int k)
    {
    int i = 0;
    while(i<k)
    {
    Console.WriteLine(sortedStockList.ElementAt(sortedStockList.Count - i).name);
    }
    }


    }











share







New contributor




AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    Please suggest way to solve a coding question, I came across. Question is as follows:



    Implement a class StockMarket that has the two following functions:



     class StockMarket
    {

    public:
    void addTrade(string stockName, int share);
    - This function keeps track the number of shares transacted for a stock by adding the trade information
    void printTop(int numberOfStock);
    - This function prints the top numberOfStock stocks that has the highest number of transacted shares

    }


    And here's a sample calling sequence that uses the above class:



     StockMarket stockMarket = new StockMarket();
    stockMarket.addTrade("GOOGLE", 50);
    stockMarket.addTrade("APPLE", 150);
    stockMarket.addTrade("GOOGLE", 100);
    stockMarket.addTrade("MSFT", 250);
    stockMarket.addTrade("GOOGLE", 200);

    stockMarket.printTop(2);


    Expected output:
    GOOGLE 350
    MSFT 250



    I followed 2 approaches to solve this problem:




    1. Create Dictionary with Key - CompanyName and value - number of shares. While giving top results, I sorted dictionary by descending and returned results. This



    2. Create SortedSet with custom comparer to sort stocks with descending order. I am facing challanges to update SortedSet. I am not able to remove old entries with time less than O(n). Please find below my current code :



      public class StockContainer
      {
      public Dictionary map;
      public SortedSet sortedStockList;



      public StockContainer()
      {
      map = new Dictionary<string, StockNode>();
      sortedStockList = new SortedSet<StockNode>(new StockNodeComparer());
      }

      public void AddValue(string s, int x)
      {
      if(map.ContainsKey(s))
      {
      int tempVal = map[s].value;
      sortedStockList.Remove(map[s]);
      var tempNode = new StockNode(s, tempVal + x);
      AddNodeInDict(s, ref tempNode);
      sortedStockList.Add(tempNode);
      }
      else
      {
      var tempNode = new StockNode(s, x);
      AddNodeInDict(s, ref tempNode);
      sortedStockList.Add(tempNode);
      }
      }

      private void AddNodeInDict(string s, ref StockNode tempNode)
      {
      map[s] = tempNode;
      }

      public void GetTop(int k)
      {
      int i = 0;
      while(i<k)
      {
      Console.WriteLine(sortedStockList.ElementAt(sortedStockList.Count - i).name);
      }
      }


      }











    share







    New contributor




    AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      Please suggest way to solve a coding question, I came across. Question is as follows:



      Implement a class StockMarket that has the two following functions:



       class StockMarket
      {

      public:
      void addTrade(string stockName, int share);
      - This function keeps track the number of shares transacted for a stock by adding the trade information
      void printTop(int numberOfStock);
      - This function prints the top numberOfStock stocks that has the highest number of transacted shares

      }


      And here's a sample calling sequence that uses the above class:



       StockMarket stockMarket = new StockMarket();
      stockMarket.addTrade("GOOGLE", 50);
      stockMarket.addTrade("APPLE", 150);
      stockMarket.addTrade("GOOGLE", 100);
      stockMarket.addTrade("MSFT", 250);
      stockMarket.addTrade("GOOGLE", 200);

      stockMarket.printTop(2);


      Expected output:
      GOOGLE 350
      MSFT 250



      I followed 2 approaches to solve this problem:




      1. Create Dictionary with Key - CompanyName and value - number of shares. While giving top results, I sorted dictionary by descending and returned results. This



      2. Create SortedSet with custom comparer to sort stocks with descending order. I am facing challanges to update SortedSet. I am not able to remove old entries with time less than O(n). Please find below my current code :



        public class StockContainer
        {
        public Dictionary map;
        public SortedSet sortedStockList;



        public StockContainer()
        {
        map = new Dictionary<string, StockNode>();
        sortedStockList = new SortedSet<StockNode>(new StockNodeComparer());
        }

        public void AddValue(string s, int x)
        {
        if(map.ContainsKey(s))
        {
        int tempVal = map[s].value;
        sortedStockList.Remove(map[s]);
        var tempNode = new StockNode(s, tempVal + x);
        AddNodeInDict(s, ref tempNode);
        sortedStockList.Add(tempNode);
        }
        else
        {
        var tempNode = new StockNode(s, x);
        AddNodeInDict(s, ref tempNode);
        sortedStockList.Add(tempNode);
        }
        }

        private void AddNodeInDict(string s, ref StockNode tempNode)
        {
        map[s] = tempNode;
        }

        public void GetTop(int k)
        {
        int i = 0;
        while(i<k)
        {
        Console.WriteLine(sortedStockList.ElementAt(sortedStockList.Count - i).name);
        }
        }


        }











      share







      New contributor




      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Please suggest way to solve a coding question, I came across. Question is as follows:



      Implement a class StockMarket that has the two following functions:



       class StockMarket
      {

      public:
      void addTrade(string stockName, int share);
      - This function keeps track the number of shares transacted for a stock by adding the trade information
      void printTop(int numberOfStock);
      - This function prints the top numberOfStock stocks that has the highest number of transacted shares

      }


      And here's a sample calling sequence that uses the above class:



       StockMarket stockMarket = new StockMarket();
      stockMarket.addTrade("GOOGLE", 50);
      stockMarket.addTrade("APPLE", 150);
      stockMarket.addTrade("GOOGLE", 100);
      stockMarket.addTrade("MSFT", 250);
      stockMarket.addTrade("GOOGLE", 200);

      stockMarket.printTop(2);


      Expected output:
      GOOGLE 350
      MSFT 250



      I followed 2 approaches to solve this problem:




      1. Create Dictionary with Key - CompanyName and value - number of shares. While giving top results, I sorted dictionary by descending and returned results. This



      2. Create SortedSet with custom comparer to sort stocks with descending order. I am facing challanges to update SortedSet. I am not able to remove old entries with time less than O(n). Please find below my current code :



        public class StockContainer
        {
        public Dictionary map;
        public SortedSet sortedStockList;



        public StockContainer()
        {
        map = new Dictionary<string, StockNode>();
        sortedStockList = new SortedSet<StockNode>(new StockNodeComparer());
        }

        public void AddValue(string s, int x)
        {
        if(map.ContainsKey(s))
        {
        int tempVal = map[s].value;
        sortedStockList.Remove(map[s]);
        var tempNode = new StockNode(s, tempVal + x);
        AddNodeInDict(s, ref tempNode);
        sortedStockList.Add(tempNode);
        }
        else
        {
        var tempNode = new StockNode(s, x);
        AddNodeInDict(s, ref tempNode);
        sortedStockList.Add(tempNode);
        }
        }

        private void AddNodeInDict(string s, ref StockNode tempNode)
        {
        map[s] = tempNode;
        }

        public void GetTop(int k)
        {
        int i = 0;
        while(i<k)
        {
        Console.WriteLine(sortedStockList.ElementAt(sortedStockList.Count - i).name);
        }
        }


        }









      c#





      share







      New contributor




      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 min ago









      AkshayPAkshayP

      1




      1




      New contributor




      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      AkshayP is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          AkshayP is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211794%2fimplement-system-to-get-most-traded-stocks-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          AkshayP is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          AkshayP is a new contributor. Be nice, and check out our Code of Conduct.













          AkshayP is a new contributor. Be nice, and check out our Code of Conduct.












          AkshayP is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211794%2fimplement-system-to-get-most-traded-stocks-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀