Solutions to all sample problems on HackerRank under the Java domain can be looked up here.
Programming is a very hands process and is both an art as well as a science. We are engineers and are required to create efficient solutions but at the same time our programs should be highly readable and flexible and all the other snappy terms which makes it an art as well.
To become proficient in this art, there are many resources, and books and tutorials. Each has it's merit and making the first step in any direction is commendable, but the cardinal factor at the end of the day will be you sitting down (or standing) and writing code. No book or resource can substitute that.
So, what are you waiting for ππ - try as many questions (below or otherwise) as you can....
- Java Introduction
- Printing values and Taking input
- Variables and Data Types
- Operators and Arithmetic
- Conditional Statements
- Comments
- Import Statements
Problem | Source | Solution Link |
---|---|---|
Welcome to Java | HackerRank | Solution |
Java StdIn and StdOut I | HackerRank | Solution |
Java If Else | HackerRank | Solution |
Data Types Problem | HackerRank | Solution |
Java Int To String | HackerRank | Solution |
Java Currency Formatter Problem | HackerRank | Solution |
Java String Reverse | HackerRank | Solution |
- While Loop
- For Loop
- Break Statement
- Fibonacci Series
Problem | Source | Solution Link |
---|---|---|
Java Output Formatting | HackerRank | Solution |
Java Strings Introduction | HackerRank | Solution |
Java Substring | HackerRank | Solution |
Java String Compare | HackerRank | Solution |
Java Date and Time | HackerRank | Solution |
Loop based & Pattern based questions given here
Problem No. | Solution |
---|---|
Question 1 | Solution |
Question 2 | Solution |
Question 3 | Solution |
Question 4 | Solution |
Question 5 | Solution |
Question 6 | Solution |
Question 7 | Solution |
Question 8 | Solution |
Question 9 | Solution |
Question 10 | Solution |
Question 11 | Solution |
Question 12 | Solution |
Question 13 | Solution |
Question 14 | Solution |
Question 15 | Solution |
Question 16 | Solution |
Question 17 | Solution |
Question 19 | Solution |
- For Loop
- Nested For Loops
- Pattern Printing Questions
- Question 18 of the loops section of begin with java.
Problem | Solution Link |
---|---|
Question 18. a | Solution |
Question 18. b | Solution |
Question 18. c | Solution |
Question 18. d | Solution |
Question 18. e | Solution |
Question 18. f | Solution |
- WAP (Write a program) to a print the following pattern. User enters
rows
[See Solution]
********
*** ***
** **
* *
* *
** **
*** ***
********
- WAP to print the following pattern. User will enter number of rows (
rows
>= 2). If user entersrows
< 2 then no pattern is printed. You can also add a second variable calledcolumns
that takes how many stars*
to print in the first and last line. [See Solution]
********
* *
* *
* *
* *
********
- WAP to print the following pattern, User enters
rows
[See Solution]
*****
* *
* *
*
- WAP to print the following pattern. User enters
rows
. [See Solution]
* * * * *
* * * *
* * *
* *
*
- WAP to print the following pattern. User enters
rows
. [See Solution]
*
* *
* * *
* * * *
* * * * *
- WAP to print the following pattern. User enters
rows
. [See Solution]
*
* *
* *
* *
* * * * *
- WAP to print the following pattern. User enters
rows
. [See Solution]
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
- WAP to print the following pattern. User enters
rows
. [See Solution]
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
- WAP to print the following pattern. User enters
rows
. [See Solution]
1
1 2
1 x 3
1 x x 4
1 x x x 5
1 2 3 4 5 6
- WAP to print the following pattern. User enters
rows
. [See Solution]
A
A B A
A B C B A
A B C D C B A
- WAP to print the following patter. User enters
rows
. [See Solution]
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
*8*8*8*8*8*8*8*8*
- WAP to print the following patter. User enters
rows
. [See Solution]
*
* *
* * *
* *
*
- WAP to print the following pattern. User enters
rows
. [See Solution]
*
* *
* *
* *
* *
* *
* *
* *
*
- WAP to print the following pattern. User enters
rows
. [See Solution]
*
**
***
****
***
**
*
In case rows = 4
*
**
**
*
- WAP to print the following pattern. User enters
rows
. [See Solution]
For row = 3
3
44
555
6666
555
44
3
For row = 2
2
33
444
33
2
For row=5
5
66
777
8888
99999
101010101010
99999
8888
777
66
5
- WAP to print the following pattern. User enters
rows
. [See Solution]
For rows = 4
.
1
2 3
4 5 6
7 8 9 10
For row = 2
1
2 3
- WAP to print the following pattern. User enters
rows
. Forrows = 3
[See Solution]
1
2*3
4*5*6
4*5*6
2*3
1
for row = 4
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
- WAP to print the following pattern (Pascal's Triangle). User enters
rows
. [See Solution]
For rows = 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
The pattern is derived in the pascal's triangle using values in the previous row. e.g.
1
1 1
1 (1 + 1) 1
1 (1 + 2) (2 + 1) 1
1 (1 + 3) (3 + 3) (3 + 1) 1
- WAP to print the following pattern (Hollow Rhombus). [See Solution]
For row=3
******
** **
* *
* *
** **
******
For row = 5
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** *****
**********
- WAP to print the following pattern (Butterfly pattern). [See Solution]
For row = 3
* *
** **
******
******
** **
* *
For row = 5
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *
- Switch Statement
- Amortised Code Analysis (Big-O Notation)
- Functions
- java.Math Module
- Time Complexity Problems [InterviewBit]
- WAP to create a function
add()
that takes 2 integer values and returns the sum.
public static int add(int a, int b) {
}
- WAP to create a function
difference()
that takes 2 integer values and returns the difference.
public static int difference(int a, int b) {
}
- WAP to create a function
product()
that takes 2 integer values and returns the product.
public static int product(int a, int b) {
}
- WAP to create a function
divide()
that takes 2 integer values and returns the quotient.
public static int divide(int a, int b) {
}
- WAP that creates a function which calculates the factorial of a number
n
. Where:
0! = 1
1! = 1
3! = 1 * 2 * 3 = 6
5! = 1 * 2 * 3 * 4 * 5 = 120
- WAP to create a function that calculates the Binomial Coefficient
nCr
. Where:
nCr = n! / (r! * (n-r)!)
4C0 = 4! / 0! 4! = 1
3C2 = 3! / 2!*1! = 6 / 2 = 3
- WAP to create a function that calculates the Permutation value
nPr
. Where
nPr = n! / (n-r)!
5P0 = 5! / 5! = 1
6P3 = 6! / 3! = 6*5*4 = 120
- WAP to create a function to check whether a given number is prime or not.
private static boolean isPrime(int number) {
}
- WAP to create a function that returns the length of a hypotenuse of a right angle triangle given the lengths of the other 2 sides
private static double hypotenuseLength(double a, double b) {
}
- WAP to create a function to return the day of the week (name) from the day number. Example
private static String dayFrom(int value) {
}
dayFrom(0) --> "Sunday"
dayFrom(1) --> "Monday"
...
...
dayFrom(6) --> "Saturday"
- Functions and methods continued
- Function Polymorphism
- Method Overriding
- Recursion
-
WAP to return the value of the sum of the following series recursively given
n
. [See Solution]1^1 + 2^2 + 3^3 + 4^4 + ... + n^n
-
WAP a program that returns that sum of the first N natural numbers and finds this sum recursively. [See Solution]
f(N) = 1 + 2 + 3 + ... + N
so, for
N = 3
f(3) = 1 + 2 + 3 = 6
and forN = 5
f(5) = 15
. -
WAP a program that calculates the sum of the series
f(N) = 1 + 2^2 + 3^2 + ... + N^2
using a recursive function. [See Solution]
- 1D Arrays
- Varargs Methods
- Foreach Loop
- 2D Arrays
-
WAP to input an integer number
length
from the user and then instantiate an integer array of sizelength
and then display that array on the terminal with all entered values.
[See Solution] -
WAP that inputs an integer array from the user and returns the sum of all its elements. Create a dedicated method for calculating array sum with the following signature. [See Solution]
private static int sum(int[] array);
-
WAP that inputs an integer array from the user and prints the number of even, odd and zero elements. [See Solution]
-
WAP to implement linear searching in an array. The user enters an array of size
n
and then an integerelement
. search for thiselement
in the array and return the first index at which it is found otherwise return -1 if not found in the array. [See Solution] -
WAP that takes an array from the user and returns a reverse array with the order of all elements reversed and then prints this reversed array. [See Solution]
If the input array is array = [1, 2, 3, 4, 5] then the resultant array should be = [5, 4, 3, 2, 1]
The method should have the following signature.
private static int[] reverse(int[] array);
-
WAP that takes an integer array from the user and returns the product of all it's elements. [See Solution] The method signature should be
private static long product(int[] array);
-
WAP to tell whether an array is an anagram or not. The user will enter an integer array. [See Solution] For example
array = [1, 2, 1] this is an anagram as the reverse of this array is also [1, 2, 1] array = [1, 1, 1, 1] This is also an anagram as the reverse is the same [1, 1, 1, 1] array = [] The empty array is also an anagram as the reverse is [] the same. array = [-1, 0] is not an aanagram as the reverse = [0, -1] isn't the same.
Crete a method with the following signature that returns boolean; true or false i it is an anagram or not.
private static boolean isAnagram(int[] array);
-
WAP to Merge 2 sorted arrays and return the new array which is also sorted. [See Solution] For example
Let there be 2 arrays first = [1, 10, 20] and second = [1, 2, 4, 10, 67] Define a function with a sigature as:
private static int[] merge(int[] first, int[] second);
which should return an array = [1, 1, 2, 4, 10, 10, 20, 67]
- Object Oriented Programming Basics
- Classes and Objects
- Constructors
- Default Constructors
- Parameterized Constructors
- Constructor Overloading
- Access Modifier
- The Final Keyword
Problem | Platform | Solution Link |
---|---|---|
Java Inheritance I | HackerRank | Solution |
Java Inheritance II | HackerRank | Solution |
Java Abstract Class | HackerRank | Solution |
Java Interface | HackerRank | Solution |
Java Method Overriding | HackerRank | Solution |
Java Overriding II (Super Keyword) | HackerRank | Solution |
- OOP Continued
- Class Inheritance
- The Java Object Class
- Java Interfaces
- Dynamic Binding
- The Java Collection Interface
- The Java ArrayList Class
- Example ArrayList
- Example HashSet
Try the problems given below and have a look at the solution only after you have solved it to see the different implementations and ideas we might've used or to use as a reference if unable to solve yourself.
Problem | Platform | Solution Link |
---|---|---|
Java List | HackerRank | Solution |
Java ArrayList | HackerRank | Solution |
Java Comparator | HackerRank | Solution |
Covariant Return Types | HackerRank | Solution |
- @Override decorator
- Overriding toString() and equals() method from the java.lang.Object Object
- Collections Interface
- List Interface
- Arraylist class
- Stack Class
- Queue Interface
- LinkedList Class
- Priority Queue
- Iterable Interface
- Iterators
- Comparator Interface
- compareTo() method
Try the problems given below and have a look at the solution only after you have solved it to see the different implementations and ideas we might've used or to use as a reference if unable to solve yourself.
Problem | Platform | Solution Link |
---|---|---|
Java Stack | HackerRank | Solution |
Java Priority Queue | HackerRank | Solution |
Maximum Element | HackerRank | Solution |
Equal Stacks | HackerRank | Solution |
Balanced Brackets | HackerRank | Solution |
Problem | Platform | Solution Link |
---|---|---|
Java Anagrams | HackerRank | Solution |
Java Hashset | HackerRank | Solution |
Java Map | HackerRank | Solution |
Pair Sums | HackerRank | Solution |