Skip to main content

Top 15 Java Coding Interview Questions for Product-Based Companies

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.

Comments

Popular posts from this blog

Top 15 React Interview Questions for 1–2 Years Experience

🟦 Top 15 React Interview Questions for 1–2 Years Experience Preparing for a React interview with 1–2 years of experience? Here's a carefully curated list of 15 important React questions with clear, real-world answers. These are frequently asked in interviews at companies like TCS, Infosys, Cognizant, Capgemini, and product-based firms. Q1. What is the Virtual DOM in React, and how does it improve performance? Answer: The Virtual DOM is a lightweight, in-memory copy of the real DOM. When state/props change, React creates a new Virtual DOM tree, compares it with the old one (diffing), and only updates the parts of the real DOM that changed. This makes rendering much faster and improves performance in large applications. Q2. What is JSX in React? Answer: JSX stands for JavaScript XML. It allows us to write HTML elements in JavaScript and place them in the DOM without using createElement() . JSX improves code readability and is transpiled to React.createElement() calls. ...

Top 15 Spring Boot Interview Questions and Answers – Real Examples (2025)

Top 15 Spring Boot Interview Questions – 2025 Spring Boot is one of the most demanded frameworks for Java backend development. Whether you're interviewing for TCS, Infosys, or a product-based company, these Spring Boot questions will help you prepare like a pro. Here are 15 questions with detailed explanations for developers with 1–2 years of experience. Q1. What is Spring Boot? Answer: Spring Boot is a Java-based open-source framework built on top of the Spring Framework. It helps developers create stand-alone, production-ready Spring applications with minimal configuration. Its key features include: Auto-configuration Embedded servers (Tomcat, Jetty) Starter dependencies Production-ready tools (Actuator, Metrics, etc.) Example: You can create a REST API within minutes by using @RestController and spring-boot-starter-web — no need for external web server deployment. Q2. What is the role of @SpringBootApplication annotation? Answer: This annotation i...

Wipro Java Developer Interview Questions with Answers (Mid-Level Role)

  Wipro Java Developer Interview Questions with Answers (Mid-Level Role) (Glassdoor Based – May 2024) Interview Location: Bengaluru Interview Mode: Online Candidate Role: Mid-Level Java Developer Source: Based on real experience shared on Glassdoor Review Summary: Easy and conversational. Interviewer was friendly. Focus was mainly on Java basics, internals, and real-world understanding. Q1: What is static in public static void main(String[] args) ? A: The static keyword lets the JVM call the method without creating an object. It indicates that the method belongs to the class, not instances. Q2: Why does a Java program start from the main method? A: main() is the predefined entry point of a Java application. The JVM starts execution from there. Q3: What are Checked and Unchecked Exceptions? With examples. A: Checked Exceptions : Detected at compile time. E.g., IOException , SQLException . Unchecked Exceptions : Detected at runtime. E.g., NullPointerExce...