How to use the Java Practice Problems
- 1. Pick a problemFilter by level and choose a problem. Its task and sample tests appear below.
- 2. Write and run it locallyWrite your Java solution here (it's saved in this browser), then run it with javac and java on each sample input.
- 3. Check your outputPaste what your program printed for each test and press Check. Solved problems get a tick.
What you'll practise
The problems move from basics (reversing a string, palindrome numbers, FizzBuzz, leap years) to the collections interviewers love (character counts with TreeMap, duplicates with HashSet, grouping anagrams with LinkedHashMap), algorithms (Kadane's maximum subarray, the missing number, a sieve of Eratosthenes), object-oriented design (a BankAccount with encapsulated balance, sorting students with a Comparator), and the Stream API.
Every solution uses a public class Main and was compiled with javac and run against its sample tests.
Worked example: bank account class
The input opens an account with 1000, then runs three operations: withdraw 300, withdraw 900 and deposit 50. The expected output is Insufficient funds followed by 750: the first withdrawal leaves 700, the second is refused because 900 is more than 700, and the deposit brings the balance to 750. The point of the exercise is encapsulation: the balance is private, and only deposit and withdraw can change it.
Tips for Java coding rounds
- Read input with
Scannerfor practice; for very large inputs,BufferedReaderis faster. - Use
longwherever sums can pass about 2.1 billion. - Build big outputs in a
StringBuilderand print once. - Before submitting anywhere, test the edge cases the samples hint at: an empty line, a single element, all-negative numbers or the largest allowed input.
Frequently asked questions
Which Java version do I need?
Java 17 or later; one solution uses a record.
Why must the class be called Main?
Most online judges and the run command here expect it. If you rename it, rename the file too.
Is my code sent anywhere?
No. It stays in this browser.
Is the Java practice problems free?
Yes. Everything in the Java practice problems works without signing up. The first time you ask for a result, we ask once for your name and WhatsApp number; after that the Java practice problems and every other Uncodemy tool open straight away.