Hotel Prices in C++ Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Hotel Prices 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.

Hotel Prices in C++ Hacker Rank Solution
Hotel Prices 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.

Hotel Prices in C++ Hacker Rank Solution

Problem

In this challenge, the task is to debug the existing code to successfully execute all provided test files
The given code defines two classes HotelRoom and HotelApartment denoting respectively a standard hotel room and a hotel apartment. An instance of any of these classes has two parameters: bedrooms and bathrooms denoting respectively the number of bedrooms and the number of bathrooms in the room.

The prices of a standard hotel room and hotel apartment are given as:

  • Hotel Room: 50.bedrooms + 100.bathrooms.
  • Hotel Apartment: The price of a standard room with the same number bedrooms and bathrooms plus 100.

For example, if a standard room costs 200, then an apartment with the same number of bedrooms and bathrooms costs 300.
In hotel’s codebase, there is a piece of code reading the list of rooms booked for today and calculates the total profit for the hotel. However, sometimes calculated profits are lower than they should be.

Debug the existing HotelRoom and HotelApartment classes’ implementations so that the existing code computing the total profit returns a correct profit.

Your function will be tested against several cases by the locked template code.


Input Format :

The input is read by the provided locked code template.
In the first line, there is a single integer n denoting the number of rooms booked for today.
After that n lines follow. Each of these lines begins with a room_type which is either standard or apartment, and is followed by the number of bedrooms and the number of bathrooms in this room.

Constraints :

  • 1 <= n <= 100
  • There is at least 1 and at most 5 bedrooms in a room
  • There is at least 1 and at most 5 bathrooms in a room

Output Format :

The output is produced by the provided and locked code template. It calculates the total profit by iterating through the vector of all rooms read from the input.


Sample Input :

2
standard 3 1
apartment 1 1

Sample Output :

500

Explanation :

In the sample we have one standard room with 3 bedrooms and 1 bathroom, and one apartment with one 1 bedrooms and 1 bathroom. The price for the room is 3.50+100 = 250 . The price for the apartment is 50+100+100 = 250. Thus the hotel profit is 250+250 = 500 as the sum of prices of both rooms.



Hotel Prices in C++ Hacker Rank Solution

#include <iostream>
#include <vector>
using namespace std;
class HotelRoom
{
    public:
        HotelRoom(int bedrooms, int bathrooms)
        : bedrooms_(bedrooms), bathrooms_(bathrooms) {}
        int get_price()
        {
            return 50*bedrooms_ + 100*bathrooms_;
        }
    private:
        int bedrooms_;
        int bathrooms_;
};
class HotelApartment : public HotelRoom
{
    /* Hotel Prices in C++ - Hacker Rank Solution START */
    /* only change this line
        HotelApartment(int bedrooms, int bathrooms)
        : HotelRoom(bedrooms, bathrooms)
      TO
        HotelApartment(int bedrooms, int bathrooms)
        : HotelRoom(bedrooms + 2, bathrooms)
    */
    public:
        HotelApartment(int bedrooms, int bathrooms)
        : HotelRoom(bedrooms + 2, bathrooms) {}
    /* END Hotel Prices in C++ - Hacker Rank Solution END */
        int get_price()
        {
            return HotelRoom::get_price() + 100;
        }
};
int main()
{
    int n;
    cin >> n;
    vector<HotelRoom*> rooms;
    for (int i = 0; i < n; ++i)
    {
        string room_type;
        int bedrooms;
        int bathrooms;
        cin >> room_type >> bedrooms >> bathrooms;
        if (room_type == "standard")
        {
            rooms.push_back(new HotelRoom(bedrooms, bathrooms));
        }
        else
        {
            rooms.push_back(new HotelApartment(bedrooms, bathrooms));
        }
    }
    int total_profit = 0;
    for (auto room : rooms)
    {
        total_profit += room->get_price();
    }
    cout << total_profit << endl;
    for (auto room : rooms)
    {
        delete room;
    }
    rooms.clear();
    return 0;
}

Disclaimer: The above Problem (Hotel Prices in C++ ) 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