Java interview questions

Want to become an expert in cracking Java interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public Java interview questions test.

To use our service for testing developers, purchase one of the paid plans for companies.

1. Palindrome
Java Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, isPalindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.

Solve question

2. UserInput
Java OOP Public

User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput, which accepts only digits.

Implement the class TextInput that contains:

  • Public method void add(char c) - adds the given character to the current value
  • Public method String getValue() - returns the current value

Implement the class NumericInput that:

  • Inherits TextInput
  • Overrides the add method so that each non-numeric character is ignored

For example, the following code should output "10":

TextInput input = new NumericInput();
Solve question

3. BinarySearchTree
Java Algorithmic thinking Data structures Public

Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.

Write a function that checks if a given binary search tree contains a given value.

For example, for the following tree:

  • n1 (Value: 1, Left: null, Right: null)
  • n2 (Value: 2, Left: n1, Right: n3)
  • n3 (Value: 3, Left: null, Right: null)

Call to contains(n2, 3) should return true since a tree with root at n2 contains number 3.

Solve question

4. TwoSum
Java Algorithmic thinking Searching Public

Write a function that, given a list and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. If there are no such elements, the function should return null.

For example, findTwoSum(new int[] { 1, 3, 5, 7, 9 }, 12) should return a single dimensional array with two elements and contain any of the following pairs of indices:

  • 1 and 4 (3 + 9 = 12)
  • 2 and 3 (5 + 7 = 12)
  • 3 and 2 (7 + 5 = 12)
  • 4 and 1 (9 + 3 = 12)
Solve question

5. Folders
Java Serialization Public

Implement a function folderNames, which accepts a string containing an XML file that specifies folder structure and returns all folder names that start with startingLetter. The XML format is given in the example below.

For example, for the letter 'u' and an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<folder name="c">
    <folder name="program files">
        <folder name="uninstall information" />
    <folder name="users" />

the function should return a collection with items "uninstall information" and "users" (in any order).

Solve question

6. SortedSearch
Java Algorithmic thinking Searching Public

Implement function countNumbers that accepts a sorted array of integers and counts the number of array elements that are less than the parameter lessThan.

For example, SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array elements less than 4.

Solve question

7. TrainComposition
Java Algorithmic thinking Data structures Public

A TrainComposition is built by attaching and detaching wagons from the left and the right sides.

For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.

Implement a TrainComposition that models this problem.

Solve question

8. Path
Java Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.


  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as "..".
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example, new Path("/a/b/c/d").cd("../x").getPath() should return "/a/b/c/x".

Solve question

If you feel ready, take one of our timed public Java interview questions tests:

  • HTML/CSS, JavaScript, Java, and SQL online test ( Easy  /  Hard )
  • HTML/CSS, JavaScript, SQL, and Java Algorithms online test ( Easy  /  Hard )
  • Java online test ( Easy  /  Hard )
  • Java Algorithms online test ( Easy  /  Hard )
  • Java and Android online test ( Easy  /  Hard )
  • Java and SQL online test ( Easy  /  Hard )
  • SQL and Java Algorithms online test ( Easy  /  Hard )
Not exactly what you are looking for? Go to our For developers section.