30 Java Basic Exercises for Advanced with Solutions

Master advanced Java skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in Java, setting a solid foundation for professional-level challenges. Start your journey to Java mastery today!

Learning Objectives:

By the end of these exercises, you will be equipped with advanced skills in Java, including multi-threading, file I/O, and optimization techniques for complex programs.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Find the longest substring without repeating characters

Required Input:

"abcabcbb"

Expected Output:

abc

Code In Java

import java.util.*; public class Main { public static String longestUniqueSubstring() { String str = "abcabcbb"; // Your code here return ""; } }

Output

Click Run Button to view compiled output

2. Implement a function to find all permutations of a given string

Required Input:

"abc"

Expected Output:

[abc, acb, bac, bca, cba, cab]

Code In Java

import java.util.*; public class Main { public static List findPermutations() { String str = "abc"; // Your code here return new ArrayList<>(); } }

Output

Click Run Button to view compiled output

3. Implement a function to find the median of two sorted arrays

Required Input:

[1, 3], [2]

Expected Output:

2.0

Code In Java

import java.util.*; public class Main { public static double findMedianSortedArrays(nums1 nums2) { // Your code here return 0.0; } }

Output

Click Run Button to view compiled output

4. Implement a function to calculate the square root of a number using the binary search method

Required Input:

16

Expected Output:

4

Code In Java

import java.util.*; public class Main { public static int sqrt() { int x = 16; // Your code here return 0; } }

Output

Click Run Button to view compiled output

5. Write a function to find the minimum number of platforms required at a train station given arrival and departure times of trains

Required Input:

arr = [900, 940, 950, 1100, 1500, 1800], dep = [910, 1200, 1120, 1130, 1900, 2000]

Expected Output:

3

Code In Java

import java.util.*; public class Main { public static int findPlatform() { int[] arr = {900, 940, 950, 1100, 1500, 1800}; int[] dep = {910, 1200, 1120, 1130, 1900, 2000}; // Your code here return 0; } }

Output

Click Run Button to view compiled output

6. Find the minimum number of jumps to reach the end of an array Each element in the array represents the maximum number of steps that can be jumped from that position

Required Input:

[2, 3, 1, 1, 4]

Expected Output:

2

Code In Java

import java.util.*; public class Main { public static int minJumps() { int[] arr = {2, 3, 1, 1, 4}; // Your code here return 0; } }

Output

Click Run Button to view compiled output

7. Implement a function to find the longest palindromic substring in a given string

Required Input:

"babad"

Expected Output:

aba

Code In Java

import java.util.*; public class Main { public static String longestPalindrome() { String str = "babad"; // Your code here return ""; } }

Output

Click Run Button to view compiled output

8. Find the kth largest element in an array

Required Input:

[3, 2, 1, 5, 6, 4], k = 2

Expected Output:

5

Code In Java

import java.util.*; public class Main { public static int findKthLargest() { int[] arr = {3, 2, 1, 5, 6, 4}; int k = 2; // Your code here return 0; } }

Output

Click Run Button to view compiled output

9. Implement the merge sort algorithm to sort an array

Required Input:

[12, 11, 13, 5, 6, 7]

Expected Output:

[5, 6, 7, 11, 12, 13]

Code In Java

import java.util.*; public class Main { public static void mergeSort() { int[] arr = {12, 11, 13, 5, 6, 7}; // Your code here } }

Output

Click Run Button to view compiled output

10. Write a function to detect if a cycle exists in a linked list

Required Input:

Linked list with nodes pointing as follows: 1 → 2 → 3 → 4 → 2 (cycle)

Expected Output:

true

Code In Java

import java.util.*; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public class Main { public static boolean hasCycle() { // Create the linked list with a cycle here // Your code here return false; } }

Output

Click Run Button to view compiled output

ad vertical

1 of 3