Java Coding Practice Online

The fastest way to get better at Java is to write it. On Quick Tech Learners you can practise Java in a browser editor and run your code against test cases, with no local setup.

Below are exercises that cover common interview patterns, with the approach and a reference solution to compare against after you have tried it yourself.

Practice exercises with approaches

Two Sum: return the indices of two numbers that add up to a target.

Brute force checks every pair in O(n²). Store each value's index in a HashMap as you scan, and look up target minus the current number: O(n) time, O(n) space.

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> seen = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        Integer j = seen.get(target - nums[i]);
        if (j != null) return new int[] { j, i };
        seen.put(nums[i], i);
    }
    return new int[0];
}

Check whether a string is a palindrome, ignoring case and non-alphanumeric characters.

Use two pointers from both ends, skipping characters that are not letters or digits and comparing the rest case-insensitively. O(n) time, O(1) extra space.

public boolean isPalindrome(String s) {
    int i = 0, j = s.length() - 1;
    while (i < j) {
        while (i < j && !Character.isLetterOrDigit(s.charAt(i))) i++;
        while (i < j && !Character.isLetterOrDigit(s.charAt(j))) j--;
        if (Character.toLowerCase(s.charAt(i++)) != Character.toLowerCase(s.charAt(j--))) return false;
    }
    return true;
}

Design an LRU cache.

A LinkedHashMap in access-order mode already keeps entries ordered by recent use. Override removeEldestEntry to evict when size exceeds capacity. In an interview, also be ready to build it from a HashMap plus a doubly linked list.

class LRUCache<K, V> extends LinkedHashMap<K, V> {
    private final int capacity;
    LRUCache(int capacity) {
        super(16, 0.75f, true);
        this.capacity = capacity;
    }
    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity;
    }
}

Group a list of words that are anagrams of each other.

Use a sorted-character key (or a letter-count key) in a HashMap<String, List<String>>, and add each word to the list for its key.

Find the first non-repeating character in a string.

Count characters with a LinkedHashMap (or an int[26] plus a second pass), then return the first character whose count is 1.

How to practise effectively

  • Attempt the problem yourself for a fixed time before reading the approach.
  • State the time and space complexity of your solution aloud.
  • Test edge cases: empty input, a single element, duplicates, very large values, null.
  • Revisit questions you got wrong after a few days instead of moving on.

Practice this on Quick Tech Learners

Reading answers is only the first step. Quick Tech Learners lets you turn Java exercises into active practice once you create an account and sign in.

  • Interview Insights: browse interview questions shared by the community and filter them by topic tag.
  • Practice Quiz: choose the concepts you want to revise, answer questions, and review how you did.
  • Coding Practice: write and run code in a browser editor against test cases. Pick a language, a difficulty band from Beginner to Expert, and either Practice mode (with hints) or Interview mode.
  • Study Plan and reports: set the topics you are preparing, then check performance, mastery and readiness reports to see where the gaps are.
  • Share Interview Question: add a question you were asked so other learners can practise it.