Skip to main content

Posts

Showing posts from August, 2021

Covariant Return Types in Java Method Overriding with examples

Covariant Return Types HackerRank solution in Java Java allows for Covariant Return Types, which means you can vary your return type as long you are returning a subclass of your specified return type.  Learn more about Method Overriding and Covariant Return Types with Examples : Method Overriding and covariant return type in Java The name comes from the fact that the type of the return is allowed to vary in the same direction that you subclass.   See problem description on HackerRank : Covariant Return Types Problem Description Lets see solution. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; //Complete the classes below class Flower {    String whatsYourName () {        return "I have many names and types.";    } } class Jasmine extends Flower {     String whatsYourName () {        return "Jasmine";     } } class Lily extends Flower {     String whatsYourName() {        return "Lily";     } } class Region {   

Java Instanceof keyword HackerRank Solution with explanation

Java Instanceof keyword with Examples What is instanceof keyword in java? instanceof is a binary operator used to test if an object is of a given type.  In other word, instanceof is a keyword that is used for checking if a reference variable is containing a given type of object reference or not. The result of the operation is either true or false. It's also known as type comparison operator because it compares the instance with type. Syntax : object instanceof Object (type) Problem description : In this problem we have given you three classes in the editor: Student class Rockstar class   Hacker class  In the main method, we populated an ArrayList with several instances of these classes. count method calculates how many instances of each type is present in the ArrayList. The code prints three integers, the number of instance of Student class, the number of instance of Rockstar class, the number of instance of Hacker class. See full description on HackerRank : Problem Descripti

Migratory Birds HackerRank Solution in Java

Java Solution for Migratory Birds HackerRank Problem | Find Lowest Maximum frequently Numbers in Java List or Array Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids. Example 1 : arr = [1, 1, 2, 2, 3] output = 1 There are two each of types 1 and 2, and one sighting of type 3.  Pick the lower of the two types seen twice: type 1. Example 2 : arr = [1, 2, 3, 4, 5, 4, 3, 2, 1, 3, 4] output = 3 The different types of birds occur in the following frequencies: Type 1 : 2 bird  Type 2 : 2 bird  Type 3 : 3 bird  Type 4 : 3 birds Type 5 : 1 bird  In this example we have two type of frequent birds : 3 and 4. From both 3 is lower than 4 so answer will be 3. Means we have to simply find out maximum frequent number from given list or array which value is lower than other same numbers. So lets see solutions with explanation.

Subarray Division HackerRank solution in Java with examples

Java solution for Subarray Division hackerRank Problem Problem Description : Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.   Lily decides to share a contiguous segment of the bar selected such that:  The length of the segment matches Ron's birth month, and, The sum of the integers on the squares is equal to his birth day. Determine how many ways she can divide the chocolate. Example 1 : s = [2, 2, 3, 1, 2] d = 4 m = 2 Output : 2 Lily wants to find segments summing to Ron's birth day, d = 4 with a length equaling his birth month, m = 2 In this case, there are two segments meeting her criteria: [2, 2] and [1, 3] Example 2 : Input : s = [1, 2, 1, 3, 2] d = 3 m = 2 Output : 2 Explanation : Lily wants to give Ron m = 2 squares summing to d = 3. The following two segments meet the criteria: 1 + 2 = 3 and 2 + 1 = 3 Example 3 : Input : s = [4, 5, 4, 2, 4, 5, 2, 3, 2, 1, 1, 5, 4] d = 15 m = 4 Output : 3 Explanation : Lily wants to

Between Two Sets Hackerrank solution in Java with explanation

Java solution for Between Two Sets HackerRank Problem There will be two arrays of integers. Determine all integers that satisfy the following two conditions: The elements of the first array are all factors of the integer being considered The integer being considered is a factor of all elements of the second array These numbers are referred to as being between the two arrays. Determine how many such numbers exist. So we have to all common numbers that are  Multiple of first array and   Factors of second array Example 1 : a = [2, 6] b = [24, 36] There are two numbers between the arrays : 6, 12. So answer is 2.  Explanation : Multiple of first array : 2 = 2, 4, 6, 8, 10, 12, 14 ... 6 = 6, 12, 18, 24, 30, 36 ... Factors of second array : 24 = 1, 2, 3, 4, 6, 8, 12, 24 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36 So all common numbers from both arrays are : 6 and 12, so answer is 2. Example 2 : a = [2, 4] b = [16, 32, 96] There are three numbers between the arrays : 4, 8 and 16. So answer is 3.

Method Overriding in Java with examples and Rules | covariant return type

In this blog we learn about, What is Method overriding What is covariant return type How we can use access modifier in method overriding How exception works with overriding How overriding works with static, final and var-args methods What is Method Overriding in Java? In simple terms, When a method in subclass have same method name, same arguments (parameters) and same return type same as its super or parent class its called Method Overriding. Overriding is a feature that allows a sub class or child class to provide specific method implementation that is already provided in its parent class or super class. Lets see simple example of Method Overriding. Example 1 : Method Overriding demo SuperClass.java  public class SuperClass {     // Overridden Method     public void OverRideMethod() {         System.out.println("Super or Parent Class");     } } SubClass.java public class SubClass extends SuperClass {          // Overriding Method     public void OverRideMethod() {         S

Breaking the Records HackerRank solution in Java

Java solution for Breaking best and worst Records HackerRank problem   Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there. Example 1 : scores = [12, 24, 10, 24]  Scores are in the same order as the games played. She tabulates her results as follows: Count Game Score Minimum Maximum Min Max 0 12 12 12 0 0 1 24 12 24 0 1 2 10 10 24 1 1 3 24 10 24 1 1  Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season. Example 2 : Input : scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]  Output : 2 4       S