HackerRank SOLUTION

Overload Operators HackerRank Solution C++

Problem: You are given a class – Complex. Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is: You need to overload operators + and << for the Complex class. The operator + should add complex numbers according to the rules…

VIEW SOLUTION

Accessing Inherited Functions HackerRank Solution C++

Problem: You are given three classes A, B and C. All three classes implement their own version of func. In class A, func multiplies the value passed as a parameter by 2: class A { public: A(){ callA = 0; } private: int callA; void inc(){ callA++; } protected: void func(int & a) { a = a * 2; inc(); } public: int…

VIEW SOLUTION

Multi Level Inheritance HackerRank Solution C++

Problem: This challenge is an extension of a previous challenge named Inheritance-Introduction. We highly recommend solving Inheritance-Introduction before solving this problem. In the previous problem, we learned about inheritance and how can a derived class object use the member functions of the base class. In this challenge, we explore multi-level inheritance. Suppose, we have a…

VIEW SOLUTION

Rectangle Area HackerRank Solution C++

Problem: In this challenge, you are required to compute the area of a rectangle using classes. Create two classes: Rectangle The Rectangle class should have two data fields-width and height of int types. The class should have display() method, to print the width and height of the rectangle separated by space. RectangleArea The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read…

VIEW SOLUTION

Inheritance Introduction HackerRank Solution C++

Problem: One of the important topics of Object Oriented Programming is Inheritance. Inheritance allows us to define a class in terms of another class, which allows us in the reusability of the code.Check out the code below: The class Triangle has a function called triangle(). Now we create a class derived from the base class…

VIEW SOLUTION

Vector-Erase HackerRank Solution C++

Problem: You are provided with a vector of N integers. Then, you are given 2 queries. For the first query, you are provided with 1 integer, which denotes a position in the vector. The value at this position in the vector needs to be erased. The next query consists of 2 integers denoting a range of the positions in the vector. The elements…

VIEW SOLUTION

Vector-Sort HackerRank Solution C++

Problem: You are given N integers.Sort the N integers and print the sorted order.Store the N integers in a vector.Vectors are sequence containers representing arrays that can change in size. Declaration: vector<int>v; (creates an empty vector of integers) Size: int size=v.size(); Pushing an integer into a vector: v.push_back(x);(where x is an integer.The size increases by 1 after this.) Popping the last…

VIEW SOLUTION

Classes and Objects HackerRank Solution C++

Problem: A class defines a blueprint for an object. We use the same syntax to declare objects of a class as we use to declare variables of other basic types. For example: Kristen is a contender for valedictorian of her high school. She wants to know how many students (if any) have scored higher than her in…

VIEW SOLUTION

Class HackerRank Solution C++

Problem: Classes in C++ are user defined types declared with keyword class that has data and functions . Although classes and structures have the same type of functionality, there are some basic differences. The data members of a class are private by default and the members of a structure are public by default. Along with…

VIEW SOLUTION

Structs HackerRank Solution C++

Problem: struct is a way to combine multiple fields to represent a composite data structure, which further lays the foundation for Object Oriented Programming. For example, we can store details related to a student in a struct consisting of his age (int), first_name (string), last_name (string) and standard (int). struct can be represented as You have to create…

VIEW SOLUTION

Strings HackerRank Solution C++

Problem: C++ provides a nice alternative data type to manipulate strings, and the data type is conveniently called string. Some of its widely used features are the following: Declaration: string a = “abc”; Size: int len = a.size(); Concatenate two strings: string a = “abc”; string b = “def”; string c = a + b; //…

VIEW SOLUTION

StringStream HackerRank Solution C++

Problem: In this challenge, we work with string streams. stringstream is a stream class to operate on strings. It implements input/output operations on memory (string) based streams. stringstream can be helpful in different type of parsing. The following operators/functions are commonly used here Operator >> Extracts formatted data. Operator << Inserts formatted data. Method str() Gets the contents of underlying string device…

VIEW SOLUTION

Arrays Introduction HackerRank Solution C++

Problem: An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. For arrays of a known size, 10 in this case, use the following declaration: Note Unlike C, C++ allows dynamic allocation of arrays at runtime without special calls…

VIEW SOLUTION

Pointer Solution HackerRank C++

Problem: A pointer in C++ is used to share a memory address among different contexts (primarily functions). They are used whenever a function needs to modify the content of a variable, but it does not have ownership. In order to access the memory address of a variable, val, prepend it with & sign. For example, &val returns the memory address of val. This…

VIEW SOLUTION

Functions Solution HackerRank C++

Problem: Functions are a bunch of statements glued together. A function is provided with zero or more arguments, and it executes the statements on it. Based on the return type, it either returns nothing (void) or something. The syntax for a function is return something of type `return_type`; } For example, a function to return…

VIEW SOLUTION

For Loop HacerRank Solution c++

Problem: A for loop is a programming language statement which allows code to be repeatedly executed. The syntax is expression_1 is used for intializing variables which are generally used for controlling the terminating flag for the loop. expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated. expression_3 is generally used…

VIEW SOLUTION

Small Triangles, Large Triangles solution HackerRank

Problem: You are given  triangles, specifically, their sides ai, bi and ci. Print them in the same style but sorted by their areas from the smallest one to the largest one. It is guaranteed that all the areas are different. The best way to calculate a area of a triangle with sides a, b and c is Heron’s formula:  where p = (a+b+c)/2. Input Format The…

VIEW SOLUTION

Conditional Statements HacerRank Solution c++

Problem : if and else are two of the most frequently used conditionals in C/C++, and they enable you to execute zero or one conditional statement among many such dependent conditional statements. We use them in the following ways: if: This executes the body of bracketed code starting with statement1 if condition evaluates to true.if (condition) { statement1; … } if – else: This executes…

VIEW SOLUTION

Basic Data Types HackerRank solution c++

Some C++ data types, their format specifiers, and their most common bit widths are as follows: Int (“%d”): 32 Bit integer Long (“%ld”): 64 bit integer Char (“%c”): Character type Float (“%f”): 32 bit real value Double (“%lf”): 64 bit real value ReadingTo read a data type, use the following syntax: For example, to read a character followed by a double: For the moment, we…

VIEW SOLUTION

Input and Output HackerRank solution c++

problem : ObjectiveIn this challenge, we practice reading input from stdin and printing output to stdout. In C++, you can read a single whitespace-separated token of input using cin, and print output to stdout using cout. For example, let’s say we declare the following variables: string s; int n; and we want to use cin to read the input…

VIEW SOLUTION

Loading…

Something went wrong. Please refresh the page and/or try again.

Contact us