0% found this document useful (0 votes)
21 views3 pages

Hexaware PGET Java Coding Prep

The document provides a collection of 25 essential Java coding problems along with their solutions. It includes problems such as calculating Fibonacci numbers, reversing strings, checking for palindromes, and finding prime numbers. Each problem is accompanied by a sample test case to demonstrate its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Hexaware PGET Java Coding Prep

The document provides a collection of 25 essential Java coding problems along with their solutions. It includes problems such as calculating Fibonacci numbers, reversing strings, checking for palindromes, and finding prime numbers. Each problem is accompanied by a sample test case to demonstrate its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Hexaware PGET Prep Pack: 25 Must-Know Java Coding Problems

1. Fibonacci (0-based)
int fib(int n) {
if (n <= 1) return n;
int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
// Test: fib(5) => 5

2. Reverse String
String rev(String s) {
return new StringBuilder(s).reverse().toString();
}
// Test: rev("hello") => "olleh"

3. Check Palindrome String


boolean isPalindrome(String s) {
int l = 0, r = s.length() - 1;
while (l < r) if (s.charAt(l++) != s.charAt(r--)) return false;
return true;
}
// Test: isPalindrome("madam") => true

4. Factorial
int fact(int n) {
int res = 1;
for (int i = 2; i <= n; i++) res *= i;
return res;
}
// Test: fact(5) => 120

5. Check Prime
boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if (n % i == 0) return false;
return true;
}
// Test: isPrime(7) => true

6. Print Prime Numbers up to N


void printPrimes(int n) {
for (int i = 2; i <= n; i++) {
boolean prime = true;
for (int j = 2; j <= Math.sqrt(i); j++)
if (i % j == 0) {
prime = false;
break;
}
if (prime) System.out.print(i + " ");
}
}
// Test: printPrimes(10) => 2 3 5 7

7. Count Frequency in Array


Map<Integer, Integer> countFreq(int[] arr) {
Map<Integer, Integer> m = new HashMap<>();
for (int n : arr) m.put(n, m.getOrDefault(n, 0) + 1);
return m;
}
// Test: [1,2,2,3] => {1=1, 2=2, 3=1}

8. Most Frequent Element


int mostFrequent(int[] arr) {
Map<Integer, Integer> m = new HashMap<>();
for (int n : arr) m.put(n, m.getOrDefault(n, 0) + 1);
int mf = 0, res = Integer.MAX_VALUE;
for (int k : m.keySet()) {
int f = m.get(k);
if (f > mf || (f == mf && k < res)) {
mf = f; res = k;
}
}
return res;
}
// Test: [1,2,2,3] => 2

9. Count Special Pairs (sum K)


int countPairs(int[] arr, int k) {
Map<Integer,Integer> m = new HashMap<>();
int count = 0;
for (int num : arr) {
count += m.getOrDefault(k - num, 0);
m.put(num, m.getOrDefault(num, 0) + 1);
}
return count;
}
// Test: ([1,1,1], 2) => 3

10. Missing Number (1 to N)


int missing(int[] a, int n) {
int total = n * (n + 1) / 2;
for (int val : a) total -= val;
return total;
}
// Test: [1,2,4], n=4 => 3

You might also like