Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Inherited code solution in C++ programming

YASH PAL, 31 July 202416 January 2026

HackerRank Inherited code C++ solution – In this HackerRank Inherited code problem in the C++ programming language, 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 realise 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 n is the length of the given username).

HackerRank Inherited code solution in c++ programming

HackerRank Inherited code problem solution in C++ programming.

#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;

/* Define the exception here */
class BadLengthException {
    private: 
        int n;
    public:
        BadLengthException(int errornumber) {
            n = errornumber;
        }
    
        int what() {
            return n;
        }
};


bool checkUsername(string username) {
    bool isValid = true;
    int n = username.length();
    if(n < 5) {
        throw BadLengthException(n);
    }
    for(int i = 0; i < n-1; i++) {
        if(username[i] == 'w' && username[i+1] == 'w') {
            isValid = false;
        }
    }
    return isValid;
}

int main() {
    int T; cin >> T;
    while(T--) {
        string username;
        cin >> username;
        try {
            bool isValid = checkUsername(username);
            if(isValid) {
                cout << "Valid" << 'n';
            } else {
                cout << "Invalid" << 'n';
            }
        } catch (BadLengthException e) {
            cout << "Too short: " << e.what() << 'n';
        }
    }
    return 0;
}

Second solution

#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;

/* Define the exception here */
class BadLengthException {
    private: 
        int n;
    public:
        BadLengthException(int errornumber) {
            n = errornumber;
        }
    
        int what() {
            return n;
        }
};


bool checkUsername(string username) {
    bool isValid = true;
    int n = username.length();
    if(n < 5) {
        throw BadLengthException(n);
    }
    for(int i = 0; i < n-1; i++) {
        if(username[i] == 'w' && username[i+1] == 'w') {
            isValid = false;
        }
    }
    return isValid;
}

int main() {
    int T; cin >> T;
    while(T--) {
        string username;
        cin >> username;
        try {
            bool isValid = checkUsername(username);
            if(isValid) {
                cout << "Valid" << 'n';
            } else {
                cout << "Invalid" << 'n';
            }
        } catch (BadLengthException e) {
            cout << "Too short: " << e.what() << 'n';
        }
    }
    return 0;
}
C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C++ Problems Solutions
Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes