March 26, 2024

Advanced Java Problems

Advanced Java problems for QA Automation Tester interviews

The list starts simple and moves to more complex topics such as data structures and algorithms. These problems in increasing order of difficulty are as follows:

  1. Check if a year is a leap year
  2. Find the missing number in an array of 1 to N
  3. Find the common elements between two arrays
  4. Count the number of vowels and consonants in a string
  5. Calculate the length of a string without using the length() method
  6. Swap two numbers without using a temporary variable
  7. Calculate the power of a number without using the Math class
  8. Calculate the square root of a number without using the Math class
  9. Calculate the area of a circle
  10. Find the intersection of two arrays
  11. Find the second largest element in an array
  12. Calculate the average of an array of numbers
  13. Implement a binary search algorithm
  14. Implement a bubble sort algorithm
  15. Implement a selection sort algorithm
  16. Implement an insertion sort algorithm
  17. Implement a quicksort algorithm
  18. Implement a factorial function using recursion
  19. Implement a Linked List
  20. Find the reverse of this linked list
  21. Remove duplicates from a sorted linked list
  22. Check if two strings are anagrams
  23. Convert a string to an integer
  24. Convert an integer to a string
  25. Calculate the GCD (Greatest Common Divisor) of two numbers
  26. Check if a number is a perfect square
  27. Implement a HashMap in Java
  28. Implement a HashSet in Java

March 13, 2024

Java Problem Solution: Implement a Linked List in Java


public class LinkedList {
    public static void main(String[] args) {
        // Test LinkedList
        Node head = createSampleList();
        System.out.print("Original Linked List: ");
        printLinkedList(head);

        int newData = 123; // Insert new node anywhere
        head = insertAnywhere(head, newData, 2);
        System.out.println("Linked List after inserting " + newData + " at position 5: ");
        printLinkedList(head);

        int dataToDelete = 4; // Delete node with data 4
        head = deleteNode(head, dataToDelete);
        System.out.println("Linked List after deleting node with data " + dataToDelete + ": ");
        printLinkedList(head);
    }
    static class Node {
        int data;
        Node next;
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    // Function to create a sample LinkedList
    private static Node createSampleList() {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        return head;
    }
    // Function to print the elements of a LinkedList
    private static void printLinkedList(Node head) {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
    // Function to insert a new node at any position in the LinkedList
    private static Node insertAnywhere(Node head, int newData, int position) {
        Node newNode = new Node(newData);
        if (head == null || position <= 0) {
            newNode.next = head;
            return newNode;
        }
        Node current = head;
        for (int i = 0; i < position - 1 && current.next != null; i++) {
            current = current.next;
        }
        newNode.next = current.next;
        current.next = newNode;
        return head;
    }
    // Function to delete a node with a given data value from the LinkedList
    private static Node deleteNode(Node head, int dataToDelete) {
        if (head == null) {
            return null;
        }
        if (head.data == dataToDelete) {
            return head.next;
        }
        Node current = head;
        while (current.next != null) {
            if (current.next.data == dataToDelete) {
                current.next = current.next.next;
                return head;
            }
            current = current.next;
        }
        return head;
    }
}

March 12, 2024

Java Problem Solution: Find the Intersection of two Arrays in Java


import java.util.Arrays;
public class ArrayIntersection {
    public static void main(String[] args) {
        int[] arr1 = {1, 8, 3, 4, 5};
        int[] arr2 = {4, 5, 6, 7, 8, 5, 1};
        int[] intersection = findIntersection(arr1, arr2);
        System.out.print("Intersection of arrays: ");
        for (int num : intersection) {
            System.out.print(num + " ");
        }
    }
    public static int[] findIntersection(int[] arr1, int[] arr2) {
        int len1 = arr1.length;
        int len2 = arr2.length;
        int minLength = Math.min(len1, len2);
        int maxLength = Math.max(len1, len2);
        int[] result = new int[minLength];
        int index = 0;
        // Determine the smaller and larger arrays
        int[] smallerArray = (len1 <= len2) ? arr1 : arr2;
        int[] largerArray = (len1 > len2) ? arr1 : arr2;
        for (int num : smallerArray) {
            if (contains(num, largerArray)) {
                result[index++] = num;
            }
        }
        return Arrays.copyOf(result, index);
    }
    public static boolean contains(int value, int[] arr) {
        for (int num : arr) {
            if (num == value) {
                return true;
            }
        }
        return false;
    }
}