Top 15 Java Coding Interview Questions for Product-Based Companies
Product-based companies focus more on problem-solving, data structures, algorithms, and clean Java code. These 15 Java coding questions are frequently asked in interviews at companies like Amazon, Paytm, Flipkart, PhonePe, and Zomato — especially for 1–3 years experienced Java developers.
Q1. Reverse a String without using inbuilt methods
public String reverse(String input) { StringBuilder sb = new StringBuilder(); for (int i = input.length() - 1; i >= 0; i--) { sb.append(input.charAt(i)); } return sb.toString(); }
Q2. Check if a string is a palindrome
public boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) { if (str.charAt(i++) != str.charAt(j--)) return false; } return true; }
Q3. Find the first non-repeated character in a string
public char firstNonRepeatChar(String str) { Map<Character, Integer> countMap = new LinkedHashMap<>(); for (char ch : str.toCharArray()) { countMap.put(ch, countMap.getOrDefault(ch, 0) + 1); } for (Map.Entry<Character, Integer> entry : countMap.entrySet()) { if (entry.getValue() == 1) return entry.getKey(); } return '_'; }
Q4. Check if two strings are anagrams
public boolean isAnagram(String a, String b) { char[] arr1 = a.toCharArray(); char[] arr2 = b.toCharArray(); Arrays.sort(arr1); Arrays.sort(arr2); return Arrays.equals(arr1, arr2); }
Q5. Count occurrences of characters in a string
public Map<Character, Integer> charFrequency(String str) { Map<Character, Integer> map = new HashMap<>(); for (char ch : str.toCharArray()) { map.put(ch, map.getOrDefault(ch, 0) + 1); } return map; }
Q6. Remove duplicates from a string
public String removeDuplicates(String str) { Set<Character> seen = new LinkedHashSet<>(); for (char ch : str.toCharArray()) seen.add(ch); StringBuilder sb = new StringBuilder(); for (char ch : seen) sb.append(ch); return sb.toString(); }
Q7. Find the factorial of a number using recursion
public int factorial(int n) { if (n == 0 || n == 1) return 1; return n * factorial(n - 1); }
Q8. Fibonacci series using iteration
public void fibonacci(int n) { int a = 0, b = 1; System.out.print(a + " " + b); for (int i = 2; i < n; i++) { int next = a + b; System.out.print(" " + next); a = b; b = next; } }
Q9. Find duplicate elements in an array
public Set<Integer> findDuplicates(int[] arr) { Set<Integer> seen = new HashSet<>(); Set<Integer> duplicates = new HashSet<>(); for (int num : arr) { if (!seen.add(num)) duplicates.add(num); } return duplicates; }
Q10. Sort an array without using Arrays.sort()
public int[] bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }
Q11. Find missing number in array 1 to N
public int findMissing(int[] arr, int n) { int total = n * (n + 1) / 2; int sum = Arrays.stream(arr).sum(); return total - sum; }
Q12. Find the largest and smallest number in array
public int[] findMinMax(int[] arr) { int min = arr[0], max = arr[0]; for (int num : arr) { if (num < min) min = num; if (num > max) max = num; } return new int[]{min, max}; }
Q13. Implement a custom equals() and hashCode()
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; MyClass that = (MyClass) obj; return Objects.equals(this.id, that.id); } @Override public int hashCode() { return Objects.hash(id); }
Q14. Java program to reverse a number
public int reverseNumber(int num) { int reversed = 0; while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10; } return reversed; }
Q15. Check if a number is prime
public boolean isPrime(int num) { if (num < 2) return false; for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) return false; } return true; }
🧠Pro Tip for Interviews:
- Always explain your thought process clearly
- Talk about time and space complexity
- Write readable, maintainable code with good variable names
📅 Last Updated: 20 May 2025
🔖 Bookmark InterviewYatra.com for more coding + Java interview prep.
0 Comments