Messages Order in C++ Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Messages Order in C++ Hacker Rank Solution. This problem is a part of the HackerRank C++ Programming Series.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Messages Order in C++ - Hacker Rank Solution
Messages Order in C++ – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

Messages Order in C++ Hacker Rank Solution

Problem

In real life applications and systems, a common component is a messaging system. Thea idea is that a sender sends messages to the recipient. The messages might be sent for example over the network. However, some network protocols don’t guarantee to preserve the order of sent messages while they are received by the recipient. For example, if someone sends a text messages hello, hi and what’s up, they might be received in the order what’s up, hello, hi. In many systems the expected behavior is to preserve the order, so the order of sent messages is the same as the order of received messages.

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.

Class MessageFactory is required to have an empty constructor, and implement a method Message create_message(const string& text) that is expected to return a Message object storing the value of text argument. Feel free to implement any other methods and class/instance variables of this class.

The locked code template will act as follows. First, it creates objects message_factory and recipient. These objects are of types MessageFactory and Recipient respectively. Then, it reads messages from the standard input, and then it will use the provided Network class to simulate sending the messages to the recipient. The Network class randomly shuffles the passes messages and then it passes them to the recipient using recipient.receive(const Message&) method. After all messages are delivered, the recipient uses its own method print_messages to print all the received messages to the standard output, and just before doing that, it uses its own fix_order method to fix the order of retrieved messages. For that purpose, it uses std::sort() algorithm to sort the std::vector of received messages and this is the reason your Message class implementation has to provide the < operator.


Input Format :

The input is read by the provided locked code template. It contains several lines of text messages in the order that they will be sent over the network.

Constraints :

There will be at most 10 lines in the input.
Each line will contain at most 20 characters.

Output Format :

The output should be produced by the provided locked code template and it is produced as described in details in the statement. The expected order of printed messages is the same as the one in the input.


Sample Input :

Alex
Hello Monique!
What'up?
Not much :(

Sample Output :

Alex
Hello Monique!
What'up?
Not much :(


Messages Order in C++ Hacker Rank Solution

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/* Messages Order in C++ - Hacker Rank Solution START */
class Message
{
    public:
        Message() {}
        Message(const int& p_iID, const string& p_sText) : m_sText(p_sText), m_iID(p_iID) {}
        const string& get_text()
        {
             return m_sText;
        }
        const int& get_id() const {
            return m_iID;
        }
    private:
        string m_sText;
        int m_iID;
};
bool operator<(const Message& lhs, const Message& rhs)
{
    return lhs.get_id() < rhs.get_id();
}
class MessageFactory
{
    public:
        MessageFactory() {}
        Message create_message(const string& p_sText)
        {
        return {m_iID++, p_sText};
        }
    private :
        int m_iID = 0;
};
/* Messages Order in C++ - Hacker Rank Solution END */
class Recipient
{
    public:
        Recipient() {}
        void receive(const Message& msg)
        {
            messages_.push_back(msg);
        }
        void print_messages()
        {
            fix_order();
            for (auto& msg : messages_)
            {
                cout << msg.get_text() << endl;
            }
            messages_.clear();
        }
    private:
        void fix_order()
        {
            sort(messages_.begin(), messages_.end());
        }
        vector<Message> messages_;
};
class Network
{
    public:
        static void send_messages(vector<Message> messages, Recipient& recipient)
        {
        // simulates the unpredictable network, where sent messages might arrive in unspecified order
            random_shuffle(messages.begin(), messages.end());
            for (auto msg : messages)
            {
                recipient.receive(msg);
            }
        }
};
int main()
{
    MessageFactory message_factory;
    Recipient recipient;
    vector<Message> messages;
    string text;
    while (getline(cin, text))
    {
        messages.push_back(message_factory.create_message(text));
    }
    Network::send_messages(messages, recipient);
    recipient.print_messages();
}

Disclaimer: The above Problem (Messages Order in C++ – Hacker Rank Solution ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

FAQ:

Which language is best for HackerRank?

While JavaScript is the best-known language, HackerRank also found that only 5% of respondents say it is their first programming language.

Is HackerRank good for beginners?

HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

What is C++ programming used for?

C++ is a general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or “C with Classes”. Wikipedia

Where can I find HackerRank solutions in CPP?

in this post you will get all the solutions of HackerRank CPP Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.

Sharing Is Caring

Leave a Comment