HackerRank 'Box It!' Solution

Martin Kysel · August 21, 2020

Short Problem Definition:

Design a class named Box whose dimensions are integers and private to the class.

Box It!

Complexity:

Does not apply.

Execution:

Does not apply.

Solution:
// The class should have the following functions : 
class Box {
    private:
        int l=0;
        int b=0;
        int h=0;
    public:
// Constructors: 
        // Box();
        Box() = default;
        // Box(int,int,int);
        Box(int len, int bre, int hei):
            l(len), b(bre), h(hei) {};
        // Box(Box);
        Box(Box &other):
            l(other.l), b(other.b), h(other.h) {};

        // int getLength(); // Return box's length
        int getLength() { return l;}
        // int getBreadth (); // Return box's breadth
        int getBreadth() { return b;}
        // int getHeight ();  //Return box's height
        int getHeight() { return h;}

        // long long CalculateVolume(); // Return the volume of the box
        long long CalculateVolume() { return (long long) l*b*h;}

        //Overload operator < as specified
        //bool operator<(Box& b)
        bool operator<(Box& b) {
            return this->l < b.l ||
                (this->b < b.b && this->l == b.l) ||
                (this->h < b.h && this->b == b.b && this->l == b.l);
        }
};

ostream& operator<<(ostream& out, Box& b) {
    out << b.getLength() << " ";
    out << b.getBreadth() << " ";
    out << b.getHeight() << " ";

    return out;
}

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.