Sample answers for 15 common Java interview coding problems
Here are sample answers with Java code snippets to 15 common Java coding interview problems selected from the top lists, illustrating how experienced candidates can approach these challenges:
JAVA DEVELOPER INTERVIEW QUESTION ANSWERS
Check if a String is a Palindrome
public boolean isPalindrome(String str) { return new StringBuilder(str).reverse().toString().equals(str); }
Find Factorial of a Number
public int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }
Generate Fibonacci Series
public void printFibonacci(int count) { int n1 = 0, n2 = 1; for (int i = 0; i < count; i++) { System.out.print(n1 + " "); int sum = n1 + n2; n1 = n2; n2 = sum; } }
Find Duplicate Elements in an Array
public void findDuplicates(int[] arr) { Set<Integer> seen = new HashSet<>(); for (int num : arr) { if (!seen.add(num)) { System.out.println("Duplicate found: " + num); } } }
Reverse a String
public String reverseString(String str) { return new StringBuilder(str).reverse().toString(); }
Check if Two Strings are Anagrams
public boolean areAnagrams(String s1, String s2) { char[] a = s1.toCharArray(); char[] b = s2.toCharArray(); Arrays.sort(a); Arrays.sort(b); return Arrays.equals(a, b); }
Find Missing Number in Array of 1 to n
public int findMissingNumber(int[] arr, int n) { int expectedSum = n * (n + 1) / 2; int actualSum = 0; for (int num : arr) actualSum += num; return expectedSum - actualSum; }
Check if Array is Sorted
public boolean isSorted(int[] arr) { for (int i = 1; i < arr.length; i++) { if (arr[i] < arr[i - 1]) return false; } return true; }
Find Maximum and Minimum in Array
public int[] findMaxMin(int[] arr) { int max = arr[0], min = arr[0]; for (int num : arr) { if (num > max) max = num; if (num < min) min = num; } return new int[]{max, min}; }
Find First Non-Repeating Character
public char firstNonRepeatingChar(String str) { int[] freq = new int[256]; for (char c : str.toCharArray()) freq[c]++; for (char c : str.toCharArray()) { if (freq[c] == 1) return c; } return '_'; // If none found }
Reverse Linked List
public ListNode reverseList(ListNode head) { ListNode prev = null; while (head != null) { ListNode next = head.next; head.next = prev; prev = head; head = next; } return prev; }
Check if Number is Prime
public 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; }
Find Length of Linked List
public int lengthOfList(ListNode head) { int length = 0; while (head != null) { length++; head = head.next; } return length; }
Merge Two Sorted Arrays
public int[] mergeSortedArrays(int[] a, int[] b) { int[] merged = new int[a.length + b.length]; int i=0, j=0, k=0; while (i < a.length && j < b.length) { merged[k++] = a[i] < b[j] ? a[i++] : b[j++]; } while (i < a.length) merged[k++] = a[i++]; while (j < b.length) merged[k++] = b[j++]; return merged; }
Find Maximum Subarray Sum (Kadane's Algorithm)
public int maxSubArray(int[] nums) { int maxSoFar = nums[0], maxEndingHere = nums[0]; for (int i = 1; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; }
These sample solutions illustrate clear, efficient, and commonly expected coding approaches for popular Java interview questions. Experienced candidates should also explain time and space complexity and may tailor or optimize solutions based on specific interview context. For a full set of 50 solutions, additional questions would include sorting, searching, trees, graphs, dynamic programming, design patterns, and concurrency problems. This foundational set covers a broad base of typical coding interview topics for Java developers.