Skip to content
the poor coder

Hackerrank Inherited Code Solution

Poor Man
Published
Updated
Reading time
2 min read

You inherited a piece of code that performs username validation for your company's website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you realize that nobody ever defined the exception.

The inherited code is provided for you in the locked section of your editor. Complete the code so that, when an exception is thrown, it prints Too short: n (where  is the length of the given username).

Input Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

The first line contains an integer, , the number of test cases.
Each of the  subsequent lines describes a test case as a single username string, .

Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

  • The username consists only of uppercase and lowercase letters.

Output Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

You are not responsible for directly printing anything to stdout. If your code is correct, the locked stub code in your editor will print either Valid (if the username is valid), Invalid (if the username is invalid), or Too short: n (where  is the length of the too-short username) on a new line for each test case.

Sample Input

Text
3
Peter
Me
Arxwwz

Sample Output

Text
Valid
Too short: 2
Invalid

Explanation

Username Me is too short because it only contains  characters, so your exception prints .
All other validation is handled by the locked code in your editor.

Solution in cpp#

Approach 1.

Python
/* Define the exception here */
class BadLengthException: public exception {
    int N;
    public:
    BadLengthException(int N){this->N = N;}
    int what(){return N;}
};

Approach 2.

Python
/* Define the exception here */
class BadLengthException: public exception{
private:
    int N;
public:
    BadLengthException(int N){this->N = N;};
    int what(){return N;};
};

Approach 3.

Python
/* Define the exception here */
struct BadLengthException : public exception
{
  int n;
  BadLengthException(int number)
    : n(number)
  {}

  const char * what () const throw ()
  {
    return to_string(n).c_str();
  }
};

Keep going

2020

Hackerrank Virtual Functions Solution

This problem is to get you familiar with virtual functions. Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit…

2020

Hackerrank Exceptional Server Solution

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

2020

Hackerrank Box It! Solution

Design a class named Box whose dimensions are integers and private to the class. The dimensions are labelled: length , breadth , and height . The default constructor of the class should initialize , , and to . The…

ESC