DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on • Edited on

JAVA CODING QUESTIONS FOR SPRING BOOT DEVELOPER

Q #1) Write a Java Program to reverse a string without using String inbuilt
function.

TWO POINTER ==> START AND END THERE ==> USES WHILE NOT FOR


Q #2) Write a Java Program to swap two numbers without using the third
variable.
ADD-SUB-SUB
CHANGE IN MIDDLE FINGER (B COMES)

Answer:

`public void swapNumbers(int a, int b) { 
a = a + b;  // A=A^B
b = a - b;  // B=A^B
a = a - b;  // A=A^B
System.out.println("After swap: a = " + a + ", b = " + b); 
}`
Enter fullscreen mode Exit fullscreen mode

Q 3) Write a Java Program to count the number of words in a string using
HashMap

Q #4)** Write a Java Program to iterate HashMap using While and advance for
loop.**

Q #5) Write a Java Program to find whether a number is prime or not in the most efficient way?


Q #6) Write a Java Program to find whether a string or number is
palindrome or not.


Q #7) Write a Java Program for the Fibonacci series in recursion.

Q #8) Write a Java Program to iterate ArrayList using for-loop, while-loop,
and advance for-loop.

import java.util.ArrayList;

public class ArrayListIterationExample {

    public static void main(String[] args) {

        // Create ArrayList
        ArrayList<String> list = new ArrayList<>();

        // Add elements
        list.add("Java");
        list.add("Python");
        list.add("C++");
        list.add("JavaScript");

        // 1️⃣ Using For Loop
        System.out.println("Using For Loop:");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        // 2️⃣ Using While Loop
        System.out.println("\nUsing While Loop:");
        int i = 0;
        while (i < list.size()) {
            System.out.println(list.get(i));
            i++;
        }

        // 3️⃣ Using Enhanced For Loop (Advanced For Loop)
        System.out.println("\nUsing Enhanced For Loop:");
        for (String language : list) {
            System.out.println(language);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Q #9) Write a Java Program to find the duplicate characters in a string.

import java.util.HashMap;

public class DuplicateCharacters {
    public static void main(String[] args) {

        String str = "programming";
        HashMap<Character, Integer> map = new HashMap<>();

        // Count each character
        for (char ch : str.toCharArray()) {
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }

        // Print duplicates
        System.out.println("Duplicate characters:");
        for (char ch : map.keySet()) {
            if (map.get(ch) > 1) {
                System.out.println(ch);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Q #10) Write a Java Program to find the second-highest number in an array.


Q #11) Write a Java Program to check Armstrong number.


Q #12) Write a Java Program to remove all white spaces from a string
without using replace().


Q #13) Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.


Q #14) Write a program that accepts comma-separated strings, sorts the
strings in ascending order, and outputs the concatenated string of sorted
strings.


Q #15) Given a string s, return true if s is a "good" string, or false otherwise. A string s is good if all characters that appear in s have the same number of occurrences (i.e., the same frequency).


Q #16) Given an array nums and a value val, remove all instances of that
value in-place and return the new length of the array. Do not allocate extra space for another array. You must modify the input array in-place with O(1) extra memory.


Q #17) You are given an integer array nums and an array of queries queries where queries[i] = [val, index]. For each query, add val to nums[index]. Then, return the sum of all even numbers in nums.


public class SumEvenAfterQueries {
    public static void main(String[] args) {

        int[] nums = {1, 2, 3, 4};
        int[][] queries = {{1,0}, {-3,1}, {-4,0}, {2,3}};

        int evenSum = 0;

        // Step 1: Calculate initial even sum
        for (int num : nums) {
            if (num % 2 == 0) {
                evenSum += num;
            }
        }

        // Step 2: Process queries
        for (int[] q : queries) {

            int val = q[0];
            int index = q[1];

            // If old value was even, subtract it
            if (nums[index] % 2 == 0) {
                evenSum -= nums[index];
            }

            // Update value
            nums[index] += val;

            // If new value is even, add it
            if (nums[index] % 2 == 0) {
                evenSum += nums[index];
            }

            System.out.println(evenSum);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Q #18) Given two strings s and p, find all the start indices of p's anagrams in s.


import java.util.*;

public class FindAnagrams {
    public static void main(String[] args) {

        String s = "cbaebabacd";
        String p = "abc";

        List<Integer> result = new ArrayList<>();

        int[] count = new int[26];

        // Step 1: Store frequency of p
        for (char c : p.toCharArray()) {
            count[c - 'a']++;
        }

        int left = 0, right = 0, needed = p.length();

        while (right < s.length()) {

            // If current char needed
            if (count[s.charAt(right) - 'a'] > 0) {
                needed--;
            }

            count[s.charAt(right) - 'a']--;
            right++;

            // If window size equals p length
            if (needed == 0) {
                result.add(left);
            }

            // Maintain window size
            if (right - left == p.length()) {

                if (count[s.charAt(left) - 'a'] >= 0) {
                    needed++;
                }

                count[s.charAt(left) - 'a']++;
                left++;
            }
        }

        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Q #19) Given a string s, find the length of the longest substring without
repeating characters.


import java.util.HashSet;

public class LongestSubstring {
    public static void main(String[] args) {

        String s = "abcabcbb";

        HashSet<Character> set = new HashSet<>();
        int left = 0, maxLength = 0;

        for (int right = 0; right < s.length(); right++) {

            while (set.contains(s.charAt(right))) {
                set.remove(s.charAt(left));
                left++;
            }

            set.add(s.charAt(right));
            maxLength = Math.max(maxLength, right - left + 1);
        }

        System.out.println("Longest Length: " + maxLength);
    }
}
Enter fullscreen mode Exit fullscreen mode


Q #20) Merge two sorted linked lists and return it as a new sorted list.


class ListNode {
    int val;
    ListNode next;
    ListNode(int val) {
        this.val = val;
    }
}

public class MergeSortedLists {

    public static ListNode merge(ListNode l1, ListNode l2) {

        ListNode dummy = new ListNode(0);
        ListNode current = dummy;

        while (l1 != null && l2 != null) {

            if (l1.val < l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }

            current = current.next;
        }

        // Attach remaining nodes
        if (l1 != null) current.next = l1;
        if (l2 != null) current.next = l2;

        return dummy.next;
    }
}
Enter fullscreen mode Exit fullscreen mode

Q #21) You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).


`

`
public class RotateImage {
public static void main(String[] args) {

    int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
    };

    int n = matrix.length;

    // Step 1: Transpose (swap rows & columns)
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
            int temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }

    // Step 2: Reverse each row
    for (int i = 0; i < n; i++) {
        int left = 0, right = n - 1;

        while (left < right) {
            int temp = matrix[i][left];
            matrix[i][left] = matrix[i][right];
            matrix[i][right] = temp;
            left++;
            right--;
        }
    }

    // Print rotated matrix
    for (int[] row : matrix) {
        for (int val : row) {
            System.out.print(val + " ");
        }
        System.out.println();
    }
}
Enter fullscreen mode Exit fullscreen mode

}
`
`

Q #22) Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.


Q #23) Given an integer, write a function to determine if it is a power of two.

public boolean isPowerOfTwo(int n) { 
return (n > 0) && ((n & (n - 1)) == 0); 
} 
Enter fullscreen mode Exit fullscreen mode

Q #24) Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Q #25) Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.


import java.util.*;

public class FindMissingNumbers {
    public static void main(String[] args) {

        int[] nums = {4,3,2,7,8,2,3,1};
        List<Integer> result = new ArrayList<>();

        // Step 1: Mark visited indices negative
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]) - 1;

            if (nums[index] > 0) {
                nums[index] = -nums[index];
            }
        }

        // Step 2: Find positive indices
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                result.add(i + 1);
            }
        }

        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)