Java Coding Interview Questions - Grok
[Link]/c/487d5441-d1e9-4046-baeb-19a55160f745
Thought for 56s
Beginner-Level Java Coding Questions
These are foundational questions often asked to assess basic programming skills in Java.
1. Reverse a string: Write a Java program to reverse a given string without using
built-in methods like reverse() . Sample Solution: Use a StringBuilder or loop
through characters backwards.
java
public static String reverse(String in) {
StringBuilder out = new StringBuilder();
char[] chars = [Link]();
for (int i = [Link] - 1; i >= 0; i--) {
[Link](chars[i]);
}
return [Link]();
}
2. Check if a string contains vowels: Write a program to determine if a string has
any vowels (a, e, i, o, u). Sample Solution: Use regex or loop through characters.
java
public static boolean stringContainsVowels(String input) {
return [Link]().matches(".*[aeiou].*");
}
1/6
3. Swap two numbers without a temporary variable: Implement a method to swap
two integers using arithmetic operations. Sample Solution: Add and subtract the
values.
java
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
4. Check if a number is prime: Write a function to verify if a given integer is a prime
number. Sample Solution: Check divisibility from 2 to sqrt(n).
java
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) return false;
}
return true;
}
5. Print Fibonacci sequence: Generate the first n numbers in the Fibonacci series
using recursion or iteration. Sample Solution (Recursive):
java
public static void printFibonacci(int val_1, int val_2, int num) {
if (num == 0) return;
[Link](val_1 + val_2 + " ");
printFibonacci(val_2, val_1 + val_2, --num);
}
Intermediate-Level Java Coding Questions
These involve data structures, algorithms, and OOP concepts.
2/6
6. Check if two strings are anagrams: Determine if two strings contain the same
characters in any order. Sample Solution: Sort the characters and compare.
java
public static boolean areAnagrams(String s1, String s2) {
if ([Link]() != [Link]()) return false;
char[] arr1 = [Link]();
char[] arr2 = [Link]();
[Link](arr1);
[Link](arr2);
return [Link](arr1, arr2);
}
7. Find factorial of a number: Compute the factorial using a loop or recursion.
Sample Solution:
java
public static long factorial(int num) {
long result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
8. Check if a string is a palindrome: Verify if a string reads the same forwards and
backwards using recursion. Sample Solution:
java
public static boolean isPalindrome(String word) {
String reverse = getReverseWord(word);
return [Link](reverse);
}
private static String getReverseWord(String word) {
if (word == null || [Link]()) return word;
return [Link]([Link]() - 1) + getReverseWord([Link](0,
[Link]() - 1));
}
3/6
9. Find missing number in an array: Given an array of 1 to n with one missing, find
the absent number efficiently. Sample Solution: Use sum formula.
java
public static int findMissingNum(int[] array, int n) {
int expectedSum = n * (n + 1) / 2;
int actualSum = [Link](array).sum();
return expectedSum - actualSum;
}
10. Rotate a matrix by 90 degrees: Implement matrix rotation in-place. Sample
Solution: Transpose and reverse rows.
java
public static void rotate90(int[][] a, int no) {
// Transpose
for (int i = 0; i < no; i++) {
for (int j = i; j < no; j++) {
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
// Reverse each row
for (int i = 0; i < no; i++) {
for (int j = 0, l = no - 1; j < l; j++) {
int temp = a[i][j];
a[i][j] = a[i][l];
a[i][l] = temp;
l--;
}
}
}
Advanced-Level Java Coding Questions
These test deeper knowledge, such as recursion, threads, and data manipulation.
4/6
11. Solve Tower of Hanoi: Implement the recursive solution for moving n disks.
Sample Solution:
java
public static void towerOfHanoi(char source, char auxiliary, char
destination, int numOfDisk) {
if (numOfDisk > 0) {
towerOfHanoi(source, destination, auxiliary, numOfDisk - 1);
[Link]("Move 1 disk from " + source + " to " +
destination + " using " + auxiliary + ".");
towerOfHanoi(auxiliary, source, destination, numOfDisk - 1);
}
}
12. Implement binary search recursively: Search for an element in a sorted array.
Sample Solution:
java
public static int binarySearch(int[] arr, int low, int high, int key) {
if (high >= low) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] > key) return binarySearch(arr, low, mid - 1, key);
return binarySearch(arr, mid + 1, high, key);
}
return -1;
}
13. Check if a number is sum of two primes: Find if a number can be expressed as
the sum of two prime numbers. Sample Solution: Iterate and check primes.
java
private static void findSumOfPrimes(int num) {
for (int i = 2; i <= num / 2; i++) {
if (isPrime(i) && isPrime(num - i)) {
[Link](num + " = " + i + " + " + (num - i));
}
}
}
5/6
14. Remove value from ArrayList: Write a method to remove a specific string from an
ArrayList. Sample Solution: Use removeIf or loop.
java
public static void removeValue(List<String> list, String value) {
[Link](s -> [Link](value));
}
15. Find maximum in array: Return the maximum value from an integer array. Sample
Solution: Sort or iterate.
java
public static int findMax(int[] arr) {
return [Link](arr).max().orElseThrow();
}
These questions are drawn from popular interview resources and can help prepare for
Java developer roles. Practice implementing them in an IDE, and consider edge cases
like null inputs or large data sets. For more, explore topics like multithreading, collections,
or streams.
10 web pages
6/6