I spent a week working through the HackerRank Java domain end-to-end. These problems are a great warm-up for interviews and a solid way to revisit Java fundamentals โ data types, collections, BigInteger arithmetic, and classic data structure problems. Here's the full set of solutions.
Basics & I/O
Welcome to JavaBasic Hello World output
Stdin & Stdout IRead 3 integers with Scanner
Stdin & Stdout IIRead int, double and String in order
Output FormattingFormatted table with border separators
Int to StringConvert using String.valueOf()
Control Flow & Loops
// Java If-Else โ "Weird or Not Weird"
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String result;
if (n % 2 != 0) {
result = "Weird";
} else if (n >= 2 && n <= 5) {
result = "Not Weird";
} else if (n >= 6 && n <= 20) {
result = "Weird";
} else {
result = "Not Weird";
}
System.out.println(result);
// Java Loops โ Powers of 2
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long result = 1;
for (int i = 0; i < n; i++) result *= 2;
System.out.println(result);
}
Data Types
// Java Datatypes โ which primitive fits a long value?
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long x = sc.nextLong();
System.out.println(x + " can be fitted in:");
if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) System.out.println("* byte");
if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) System.out.println("* short");
if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) System.out.println("* int");
System.out.println("* long");
}
Big Numbers
// Java BigInteger โ add and multiply
Scanner sc = new Scanner(System.in);
BigInteger a = sc.nextBigInteger();
BigInteger b = sc.nextBigInteger();
System.out.println(a.add(b));
System.out.println(a.multiply(b));
// Java BigDecimal โ sort strings as decimals (descending)
// Key: use BigDecimal comparator to avoid lexicographic ordering
Arrays.sort(s, 0, n, (x, y) -> new BigDecimal(y).compareTo(new BigDecimal(x)));
// Java Primality Test
Scanner sc = new Scanner(System.in);
BigInteger n = sc.nextBigInteger();
System.out.println(n.isProbablePrime(10) ? "prime" : "not prime");
Strings
// Palindrome check
String s = sc.next();
String rev = new StringBuilder(s).reverse().toString();
System.out.println(s.equals(rev) ? "Yes" : "No");
// Anagram check โ compare sorted char arrays
char[] a = s.toLowerCase().toCharArray();
char[] b = t.toLowerCase().toCharArray();
Arrays.sort(a); Arrays.sort(b);
System.out.println(Arrays.equals(a, b) ? "Anagrams" : "Not Anagrams");
Collections
Java ListInsert and delete by index
Java MapName โ phone number lookup
Java StackBalanced brackets validator
Java ArrayListJagged array with queries
// Java Stack โ balanced brackets { [ ( ) ] }
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else {
if (stack.isEmpty()) { System.out.println("false"); return; }
char top = stack.pop();
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
System.out.println("false"); return;
}
}
}
System.out.println(stack.isEmpty());
2D Arrays
// Maximum hourglass sum in a 6x6 array
int max = Integer.MIN_VALUE;
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= 3; j++) {
int sum = arr[i][j] + arr[i][j+1] + arr[i][j+2]
+ arr[i+1][j+1]
+ arr[i+2][j] + arr[i+2][j+1] + arr[i+2][j+2];
max = Math.max(max, sum);
}
}
System.out.println(max);
More solutions in progress โ the series continues with advanced topics like generics, recursion and sorting algorithms.