0% found this document useful (0 votes)
2 views

10 Common Coding Interview Questions using Java

The document provides Java solutions for various programming problems including reversing a string, checking for palindromes, finding duplicate numbers, generating Fibonacci sequences, calculating factorials, checking anagrams, swapping variables, counting vowels and consonants, and determining if a number is prime. Each problem is accompanied by a code snippet and an explanation of how the solution works. The solutions utilize basic programming concepts such as loops, conditionals, and data structures like HashSet and arrays.
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)
2 views

10 Common Coding Interview Questions using Java

The document provides Java solutions for various programming problems including reversing a string, checking for palindromes, finding duplicate numbers, generating Fibonacci sequences, calculating factorials, checking anagrams, swapping variables, counting vowels and consonants, and determining if a number is prime. Each problem is accompanied by a code snippet and an explanation of how the solution works. The solutions utilize basic programming concepts such as loops, conditionals, and data structures like HashSet and arrays.
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/ 7

1.

​ Reverse a String:​

○​ Problem: Reverse a given 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.​

2.​ Check if a String is a Palindrome:​

○​ Problem: Determine if a given string reads the same backward as forward.​

○​ 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.​

3.​ Find Duplicate Numbers in an Array:​

○​ Problem: Identify duplicate numbers in an array of integers.​

○​ Solution:​

Java
import java.util.HashSet;
import java.util.Set;

public class FindDuplicates {


public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 2, 5, 6, 3};
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();
for (int number : numbers) {
if (!seen.add(number)) {
duplicates.add(number);
}
}
System.out.println("Duplicate numbers: " + duplicates);
}
}

○​ ​
Explanation: This code uses a HashSet to track seen numbers and identifies
duplicates by checking if an insertion fails.​

4.​ Generate Fibonacci Sequence:​


○​ Problem: Generate the Fibonacci sequence up to a specified number of terms.​

○​ 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.​

5.​ Calculate Factorial of a Number:​

○​ Problem: Compute the factorial of a non-negative integer.​

○​ 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.​

6.​ Check if Two Strings are Anagrams:​

○​ Problem: Determine if two strings are anagrams (contain the same characters in
a different order).​

○​ Solution:​

Java
import java.util.Arrays;

public class AnagramCheck {


public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
boolean isAnagram = areAnagrams(str1, str2);
System.out.println("Are anagrams: " + isAnagram);
}

static boolean areAnagrams(String str1, String str2) {


if (str1.length() != str2.length()) {
return false;
}
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
}

○​ ​
Explanation: The code sorts the characters of both strings and compares them to
check for anagram status.​

7.​ Swap Two Variables Without Using a Third Variable:​

○​ 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.​

8.​ Count Vowels and Consonants in a String:​

○​ Problem: Count the number of vowels and consonants in a given string.​

○​ 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.​

9.​ Check if a Number is Prime:​

○​ Problem: Determine whether a given number is prime.​

○​ 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);
}

static boolean isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
}

○​ ​
Explanation: The code checks divisibility from 2 up to the square root​

You might also like