Exceptional Server in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Exceptional Server in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Exceptional Server in C++ – Hacker Rank Solution

Problem

Your friend set up a small computational server that performs complex calculations.
It has a function that takes 2 large numbers as its input and returns a numeric result. Unfortunately, there are various exceptions that may occur during execution.

Complete the code in your editor so that it prints appropriate error messages, should anything go wrong. The expected behavior is defined as follows:

  • If the compute function runs fine with the given arguments, then print the result of the function call.
  • If it fails to allocate the memory that it needs, print Not enough memory.
  • If any other standard C++ exception occurs, print Exception: S where S is the exception’s error message.
  • If any non-standard exception occurs, print Other Exception.

Input Format :

The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines describes a test case as 2 space-separated integers, A and B, respectively.

Constraints :

  • 1 <= T <= 10^3
  • 0 <= A,B <= 2^60

Output Format :

For each test case, print a single line containing whichever message described in the Problem Statement above is appropriate. After all messages have been printed, the locked stub code in your editor prints the server load.


Sample Input :

2
-8 5
1435434255433 5

Sample Output :

Exception: A is negative
Not enough memory
2

Explanation :

-8 is negative, hence ‘Exception: A is negative’ is thrown. Since the second input is too large, ‘not enough memory’ is displayed. 2 is the server load.

Exceptional Server in C++ – Hacker Rank Solution
#include <iostream>
#include <exception>
#include <string>
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;

class Server 
{
    private:
	    static int load;
    public:
	    static int compute(long long A, long long B) 
	    {
		    load += 1;
		    if(A < 0) 
		    {
			    throw std::invalid_argument("A is negative");
		    }
		    vector<int> v(A, 0);
		    int real = -1, cmplx = sqrt(-1);
		    if(B == 0) throw 0;
		    real = (A/B)*real;
		    int ans = v.at(B);
		    return real + A - B*ans;
	    }
	    static int getLoad() 
	    {
		    return load;
	    }
};
int Server::load = 0;

int main() 
{
	int T; cin >> T;
	while(T--) {
		long long A, B;
		cin >> A >> B;

		/* Enter your code here. */
        try 
        {
            cout << Server::compute(A, B) << endl;
        } 
        catch (std::bad_alloc& error) 
        {
            cout << "Not enough memory" << endl;
        }
        catch (std::exception& error) 
        {
            cout << "Exception: " << error.what() << endl;
        }
        catch (...) 
        {
            cout << "Other Exception" << endl;
        }
        /* ------------*/
	}
	cout << Server::getLoad() << endl;
	return 0;
}

200 thoughts on “Exceptional Server in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions”

  1. Nice post. I study one thing more difficult on completely different blogs everyday. It is going to always be stimulating to read content material from other writers and follow somewhat one thing from their store. I’d choose to use some with the content on my blog whether you don’t mind. Natually I’ll give you a hyperlink in your web blog. Thanks for sharing.

    Reply
  2. Hey very nice web site!! Man .. Beautiful .. Amazing .. I will bookmark your web site and take the feeds also…I’m happy to find a lot of useful info here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

    Reply
  3. Enjoyed looking at this, very good stuff, thankyou. “Hereafter, in a better world than this, I shall desire more love and knowledge of you.” by William Shakespeare.

    Reply
  4. Hello would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you suggest a good hosting provider at a fair price? Thanks a lot, I appreciate it!

    Reply
  5. It’s a shame you don’t have a donate button! I’d definitely donate to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this website with my Facebook group. Chat soon!

    Reply
  6. Hey there! Someone in my Facebook group shared this site with us so I came to take a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Fantastic blog and outstanding style and design.

    Reply
  7. I am not sure where you are getting your information, however good topic. I must spend a while studying more or understanding more. Thank you for great info I used to be looking for this info for my mission.

    Reply
  8. Wow! This can be one particular of the most useful blogs We’ve ever arrive across on this subject. Actually Excellent. I am also a specialist in this topic therefore I can understand your hard work.

    Reply
  9. It’s really a great and useful piece of information. I am satisfied that you just shared this helpful information with us. Please stay us up to date like this. Thank you for sharing.

    Reply
  10. Thank you for the auspicious writeup. It in reality used to be a amusement account it. Glance complex to more added agreeable from you! By the way, how can we keep in touch?

    Reply
  11. how to buy mobic price [url=https://mobic.store/#]can i order generic mobic price[/url] how can i get cheap mobic without insurance

    Reply
  12. Hey would you mind letting me know which webhost you’re using?

    I’ve loaded your blog in 3 completely different browsers and
    I must say this blog loads a lot faster then most. Can you suggest a good hosting provider at a
    honest price? Thanks a lot, I appreciate it!

    Reply
  13. canadian medications [url=https://certifiedcanadapills.pro/#]canadian pharmacy online store[/url] canadian pharmacy meds reviews

    Reply
  14. buy medicines online in india [url=http://indiapharmacy.cheap/#]online pharmacy india[/url] reputable indian online pharmacy

    Reply
  15. What i do not realize is actually how you are not actually a lot more smartly-preferred than you may be now. You’re so intelligent. You already know therefore significantly in the case of this topic, produced me in my view imagine it from so many numerous angles. Its like men and women don’t seem to be interested until it is one thing to accomplish with Woman gaga! Your personal stuffs outstanding. All the time care for it up!

    Reply
  16. 保証の種類を表示しています 保証の種類を表示しています 【話題の作品をチェック】海外ドラマ・国内ドラマ情報 85.7万円 無有 【2週間無料トライアル】ディープでマニアックな世界中のホラー映画・TVシリーズ、オリジナルビデオや短編作品が≪見放題≫ 最安価格: ¥945 最安価格: ¥945 無有 最安価格: ¥3,200 サイトマップ 保証の種類を表示しています 【話題の作品をチェック】海外ドラマ・国内ドラマ情報 【話題の作品をチェック】海外ドラマ・国内ドラマ情報 【2週間無料トライアル】ディープでマニアックな世界中のホラー映画・TVシリーズ、オリジナルビデオや短編作品が≪見放題≫
    http://id.kaywa.com/viagravscialisbestco
    【医師監修】バイアグラの効果的で安全な使用方法について バイアグラ・シアリス・およびそれらのジェネリック薬は医師の処方せんによる処方薬です。EDに関する相談、治療は、医療機関を受診してください。 初診料と再診料は無料で、費用はバイアグラにかかる料金のみです。初めての方で、料金が気になる場合、同じ効果があり安い価格で購入できるジェネリックを選ぶことも良いアイデアです。 「捨てられない人」が「捨て上手」になる瞬間、人気家政婦が見た3つの法則 バイアグラと同じシルデナフィルを有効成分とし形状や大きさと添加物以外はバイアグラ同じ、水なしでも服用できる携帯性にすぐれたフィルム型のED治療薬です。

    Reply
  17. farmacia envГ­os internacionales [url=http://tadalafilo.pro/#]comprar cialis original[/url] farmacia envГ­os internacionales

    Reply

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock