10 Common Coding Interview Questions using Java
10 Common Coding Interview Questions using Java
Reverse a String:
○ Solution:
Java
public class ReverseString {
public static void main(String[] args) {
String input = "hello";
String reversed = new
StringBuilder(input).reverse().toString();
System.out.println("Reversed string: " + reversed);
}
}
○
Explanation: This code uses StringBuilder's reverse() method to reverse
the input string.
○ Solution:
Java
public class PalindromeCheck {
public static void main(String[] args) {
String input = "madam";
boolean isPalindrome = input.equals(new
StringBuilder(input).reverse().toString());
System.out.println("Is palindrome: " + isPalindrome);
}
}
○
Explanation: The code compares the original string with its reversed version to
check for palindrome property.
○ Solution:
Java
import java.util.HashSet;
import java.util.Set;
○
Explanation: This code uses a HashSet to track seen numbers and identifies
duplicates by checking if an insertion fails.
○ Solution:
Java
public class FibonacciSequence {
public static void main(String[] args) {
int n = 10; // Number of terms
int a = 0, b = 1;
System.out.print("Fibonacci sequence: " + a + ", " + b);
for (int i = 2; i < n; i++) {
int next = a + b;
System.out.print(", " + next);
a = b;
b = next;
}
}
}
○
Explanation: The code prints the Fibonacci sequence by iteratively calculating the
next term as the sum of the previous two.
○ Solution:
Java
public class FactorialCalculation {
public static void main(String[] args) {
int number = 5;
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("Factorial of " + number + " is: " +
factorial);
}
}
○
Explanation: This code calculates the factorial by multiplying all integers from 1
up to the given number.
○ Problem: Determine if two strings are anagrams (contain the same characters in
a different order).
○ Solution:
Java
import java.util.Arrays;
○
Explanation: The code sorts the characters of both strings and compares them to
check for anagram status.
○ Problem: Swap the values of two variables without using a temporary variable.
○ Solution:
Java
public class SwapVariables {
public static void main(String[] args) {
int a = 5;
int b = 10;
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping: a = " + a + ", b = "
+ b);
}
}
○
Explanation: This code swaps the values by leveraging arithmetic operations.
○ Solution:
Java
public class VowelConsonantCount {
public static void main(String[] args) {
String input = "hello world";
int vowels = 0, consonants = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if (c >= 'a' && c <= 'z') {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u') {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels + ", Consonants: "
+ consonants);
}
}
○
Explanation: The code iterates through the string, checking each character to
determine if it's a vowel or consonant.
○ Solution:
Java
public class PrimeCheck {
public static void main(String[] args) {
int number = 29;
boolean isPrime = isPrime(number);
System.out.println(number + " is prime: " + isPrime);
}
○
Explanation: The code checks divisibility from 2 up to the square root