HackerRank C++ Messages Order




In this problem, the task is to implement a software layer over the top of a network protocol sending messages in arbitrary order, in such a way that the sent messages are printed by the recipient in the order they were sent.

In the template code below, there are implementations of classes Recipient and Network.

Your task is to implement classes Message and MessageFactory according to the below specification:

Class Message is required to store a text value of type std::string and provide a public getter const string& get_text() which is expected to return this text value. Besides that, it should implement the < operator that will be used in fix_order() method of the recipient to fix the order of received messages. Feel free to implement any other methods and class/instance variables. In particular, you can implement any additional constructors, but make sure that you provide an empty constructor, i.e. the one without arguments.



  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. class Message {
  6. private:
  7. string text;
  8. static int id;
  9. int current_id;
  10. public:
  11. Message() { current_id = ++id; }
  12. Message(string t){ current_id = ++id; text=t; }
  13. const string& get_text() {
  14. return text;
  15. }
  16. // overloaded < operator
  17. bool operator < (const Message& M2) {
  18. if(current_id < M2.current_id)
  19. return true;
  20. else
  21. return false;
  22. }
  23. };
  24. int Message::id = 0;
  25. class MessageFactory {
  26. public:
  27. MessageFactory() {}
  28. Message create_message(const string& text) {
  29. Message m = Message(text);
  30. return m;
  31. }
  32. };
  33. class Recipient {
  34. public:
  35. Recipient() {}
  36. void receive(const Message& msg) {
  37. messages_.push_back(msg);
  38. }
  39. void print_messages() {
  40. fix_order();
  41. for (auto& msg : messages_) {
  42. cout << msg.get_text() << endl;
  43. }
  44. messages_.clear();
  45. }
  46. private:
  47. void fix_order() {
  48. sort(messages_.begin(), messages_.end());
  49. }
  50. vector<Message> messages_;
  51. };
  52. class Network {
  53. public:
  54. static void send_messages(vector<Message> messages, Recipient& recipient) {
  55. // simulates the unpredictable network, where sent messages might arrive in unspecified order
  56. random_shuffle(messages.begin(), messages.end());
  57. for (auto msg : messages) {
  58. recipient.receive(msg);
  59. }
  60. }
  61. };
  62. int main() {
  63. MessageFactory message_factory;
  64. Recipient recipient;
  65. vector<Message> messages;
  66. string text;
  67. while (getline(cin, text)) {
  68. messages.push_back(message_factory.create_message(text));
  69. }
  70. Network::send_messages(messages, recipient);
  71. recipient.print_messages();
  72. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube