10 Proven Java Programming Questions to Ask on Interview

Java Interview Questions

Looking to hire a Java developer? We’re here to help!

We’ve brought together a team of highly skilled Java experts to create a set of ready-made Java programming questions you can use in your interview process.

These Java interview questions have everything you need for a face-to-face interview with prospective developers: possible right and wrong answers, explanations, and even follow-up questions. You don’t need to be a Java expert to find the best Java programmers for your company – just use the resources we provide and interview with confidence!

So here are the questions and answers:

[Question #1 – Player/Weapon – OOP design]
[Question #2 – Polyline – OOP design]
[Question #3 – Run Length Encoding – Algorithms]
[Question #4 – The Bank – Multithreading]
[Question #5 – MaxStack – Data structures]
[Question #6 – Changing Interfaces – Refactoring]
[Question #7 – Algorithms]
[Question #8 – Array shuffle – Multithreading]
[Question #9 – Palindrome – Algorithms]
[Question #10 – Rectangle – Algorithms]
[BONUS – Java Online Test]

Are you employer or team-lead?

We have the solution for you!

If you are hiring Java developer, check out this Java interview test prepared by our professionals. It will allow you to identify the best talents very easily!

[Question #1 – Player/Weapon – OOP design]

Consider the following code written by one of our interns:

public class Player {
  private Weapon weapon;
  private String name;

  public Player(String name, Weapon weapon) {
    this.name = name;
    this.weapon = weapon;
  }

  public void setWeapon(Weapon weapon) {
    this.weapon = weapon;
  }

  public void action() {
    if (this.weapon.type == "knife") {
      System.out.println("Perform knife attack");
    } else {
      if (this.weapon.type == "revolver") {
        System.out.println("Perform revolver attack");
      } else {
        if (this.weapon.type == "Plasma Gun") {
          System.out.println("Perform plasma gun attack");
        }
      }
    }
  }
}

The above code breaks the design principle “Open to extension, closed to modification“.

Create a new design that does not violate this principle and allows for the addition of any arbitrary weapon type in the future.

You should first sketch a UML diagram to illustrate your design. Then implement the code and demonstrate how the principle is supported.

Why this Java question?

It is one of the java interview questions for freshers. Of course not 100% freshers, but this question tests understanding of basic design principles, and the ability to apply them. In particular, adhering to the “open to extension, closed to modification” principle helps to write decoupled, maintainable code.

Possible answers

Any candidate should be able to articulate that, in order to add a new weapon, modifications to the unrelated Player class are needed, tightly coupling the two together. They should also note that a chain of if statements like this does not scale well.

To improve the situation, the actual attack should delegate to a Weapon interface or abstract class. This is an application of the Strategy design pattern, and in UML it would look as follows:

java-interview-questions-uml-diagram

Then the action() method would simply be:

public void action(){
      this.weapon.attack();
}

Concrete implementations of Weapon should handle the particular types of attacks.

Follow-up questions

This works well as long as all details related to the attack can be encapsulated in the Weapon subclasses. However, what if the Player needs to perform different actions depending on the weapon type? For example, swing()ing a gun or fire()ing a sword is unlikely to work well. Or, what if some weapons need to be reload()ed before they can be used?

↑↑ Scroll up to the list of Java questions

[Question #2 – Polyline – OOP design]

Design a Polyline class to represent a geometric polyline. A polyline is made up of a sequence of line segments laid end to end on a plane.

Polyline

The Polyline class will be used in a concurrent environment and as such the decision was taken to make it immutable.

Design and implement a Polyline class ensuring that Polylines are immutable. Provide a method to add new Points to a Polyline.

Why this Java programming question?

Concurrent programming is notoriously difficult, but using immutable data structures where possible can make it considerably easier.

However, Java doesn’t have the concept of “immutable” built into the language; it’s the programmer’s responsibility to ensure that any data structure they think is immutable actually is.

Without the immutability requirement, this question would be “Java 101”, but writing a fully immutable data structure involves more care and consideration.

Possible answers

An immutable data structure in Java must meet the following requirements:

  • The class itself must be declared final, so subclasses cannot add mutable parts to it (even though the immutable parts would remain immutable). Junior candidates can easily overlook this point.
  • All fields must be private so they cannot be written to from outside the class. (Making fields protected in a final class is pointless.)
  • All fields must be final so they cannot be reassigned. This is not strictly necessary, but it’s a useful safeguard which is enforced by the compiler.
  • If the object is “composite” (contains other objects inside it), all contained objects must also be immutable. In this particular case, this means the Point class must meet all of the above requirements as well. Alternatively, defensive copies should be made if points are retrieved from the polyline.

Here is a possible implementation that meets all criteria:

public final class Point {
  private final float x;
  private final float y;

  public Point(float x, float y) {
    this.x = x;
    this.y = y;
  }

  public float getX() {
    return x;
  }

  public float getY() {
    return y;
  }
}

public final class Polyline {
  private final ArrayList<Point> points;

  private Polyline(ArrayList<Point> points) {
    this.points = points;
  }

  public static Polyline create() {
    return new Polyline(new ArrayList<Point>());
  }

  public Polyline add(Point point) {
    ArrayList<Point> newPoints = new ArrayList<>(this.points);
    newPoints.add(point);
    return new Polyline(newPoints);
  }

  public int size() {
    return points.size();
  }

  public Point getPoint(int index) {
    return points.get(index);
  }
}

Follow-up questions

If the candidate misses one of the requirements for immutability, invent a scenario in which immutability is violated and see how they handle it.

You could ask about the internal data structure that was chosen (here ArrayList). Alternatives are Vector (needless synchronization) and raw arrays (slightly more performant, but more complex code). It is also possible to implement Polyline itself as a singly linked list, which has various tradeoffs that are interesting to talk about.

Once they have arrived at a fully correct solution, you could also have a discussion about how to test this class.

10 Java Programming Questions to Ask on Interview (Explanations, Possible Answers, Following Questions)Click To Tweet

↑↑ Scroll up to the list of Java questions

[Question #3 – Run Length Encoding – Algorithms]

Write a method that accepts a string and performs a run length encoding on the string. Run length encoding works by looking for long runs of a character in the string and replacing the run with a pair consisting of the number of occurrences in the run, followed by the character itself. For example:

"AAABBAAAAABBBCCCCCCCCCAAAAA" -> "3A2B5A3B9C5A"

Why this technical question?

While not requiring very complex algorithms or data structures, this java technical question is still not easy to get completely right on the first try. There are several edge cases that need to be taken into account. Moreover, the question as stated is ambiguous in various ways, forcing the candidate to ask follow-up questions.

Possible answers

Before coding the solution, interview candidates are expected to ask some follow-up questions. In particular:

  • Should a run be encoded even if it makes the resulting string longer?
  • Should a run be encoded even if it doesn’t make the resulting string shorter? (This can be answered from the example: BB did become 2B.)
  • Can digits occur in the input? If so, how should they be handled?

How you answer these questions depends on the expected level of the candidate. For example, experienced candidates should be able to come up with a way to unambiguously handle digits.

Here is one possible implementation:

public static String runLength(String plain) {
  StringBuilder dest = new StringBuilder();
  for (int i = 0; i < plain.length(); i++) {
    int runLength = 1;
    while (i+1 < plain.length() &&
           plain.charAt(i) == plain.charAt(i+1)) {
      runLength++;
      i++;
    }
    dest.append(runLength);
    dest.append(plain.charAt(i));
  }
  return dest.toString();
}

Points to look out for:

  • Does the algorithm have O(n) runtime? In particular, is excessive concatenation of Strings avoided?
  • Is the end of the string handled correctly without dropping the last run?
  • Is this done without significant code duplication?
  • If digits can occur in the input, are they handled correctly? If they are handled using some kind of escaping mechanism (e.g. prefixing with \), is the escape character itself escaped properly?

Follow-up questions

As before, ask about test cases. At least the following classes of input should be represented:

  • null,
  • the empty string,
  • a string of a single character,
  • a string with a single run of a single character,
  • a string containing a run of length greater than 10,
  • a string with runs of length 1,
  • a very long string.

More could be expected, depending on the particular boundary conditions that you set previously.

↑↑ Scroll up to the list of Java questions

[Question #4 – The Bank – Multithreading]

You are given a class which implements a Bank as a singleton:

public class Bank {
    // Singleton implementation omitted for brevity's sake

    // map from account number to balance
    private Map<String, Integer> accounts = new HashMap<>();

    public int deposit(String account, int sum) throws IllegalArgumentException {
        if (sum < 0) { throw new IllegalArgumentException("sum cannot be negative"); } return accounts.put(account, accounts.get(account) + sum); } public int withdraw(String account, int sum) { if (sum > accounts.get(account)) {
            return -1;
        }
        accounts.put(account, accounts.get(account) - sum);
        return sum;
    }
}

How will this code behave in case of multithreaded environment? What are the worse outcome from client/bank point-of-view? What changes could be made to make this code thread safe?

Why this interview question?

For better or worse, a lot of high-performance Java code will need to be multithreaded. However, writing correct multithreaded code that is safe from deadlocks and race conditions is notoriously difficult. That’s why this question is one of the java interview questions for experienced professionals.

Java offers many primitives to help with this, but it takes experience to select the right one in each particular situation. Often, a tradeoff needs to be made between efficiency on the one hand, and readability and simplicity of the code on the other.

Possible answers

This is a traditional example taught in computer science classes. The most glaring problem is that anything may happen between the get() call and the corresponding put() call, so money could disappear or be created out of thin air.

For example, if two threads, A and B, both try to deposit 10 dollars to the same account, both might first call get() to read the account balance, say 100 dollars, then they both add 10 dollars and put() the sum of 110 dollars into the account; 10 dollars have now evaporated. Any interview candidate even vaguely familiar with threading should be able to point this out easily.

Candidates who are a bit more familiar with the Java standard library should also point out that HashMap is not thread-safe, so concurrent accesses to it might result in undefined behavior. They might even know that only “structural modifications” to the map are unsafe, but that is veering into the territory of trivia.

The nice thing about this java programming question is that there are many possible solutions. A junior candidate would probably make deposit and withdraw synchronized methods. While technically correct, this may lead to a performance bottleneck, because even transactions on unrelated bank accounts will have to wait for each other.

Slightly more advanced would be to use a ConcurrentHashMap to ensure thread safety of the internal data structure, but this doesn’t get around the fundamental problem of races between the get() and put() calls. To solve that, we need to synchronize on the individual account. One possible approach would be:

public class BankThreadSafe {
    // Singleton implementation omitted for brevity's sake

    // map from account number to balance
    private Map<String, Integer> accounts = new ConcurrentHashMap<>();


    public int deposit(String account, int sum) throws IllegalArgumentException {
        if (sum < 0) { throw new IllegalArgumentException("sum cannot be negative"); } synchronized (account) { return accounts.put(account, accounts.get(account) + sum); } } public int withdraw(String account, int sum) { synchronized (account) { if (sum > accounts.get(account)) {
                return -1;
            }
            accounts.put(account, accounts.get(account) - sum);
            return sum;
        }
    }
}

Follow-up questions

Synchronizing on the String account is not necessarily safe, because two Strings might refer to the same account, yet be different objects. If the candidate offers the solution shown above, ask them about that. Calling String.intern() would resolve the issue.

An alternative, perhaps cleaner approach is to wrap the account balance in a class, say Account, on which we can safely synchronize. This would also allow the future possibility of introducing a ReadWriteLock to allow for concurrent reads, which could improve performance under read-heavy loads.

↑↑ Scroll up to the list of Java questions

[Question #5 – MaxStack – Data structures]

Java has a Stack implementation which provides the push(item) and pop() methods.

Create a MaxStack class that extends this class and adds a max() method that returns the largest element in the MaxStack as an O(1) operation without damaging the Big-O complexity of the existing methods.

Why this Java question?

Big-O complexity may sound like a theoretical concept, but its practical implications are immense.

If a programmer doesn’t keep Big-O complexity in mind while writing code, their code might seem to work fine for 100 items or 1,000 items, but grind to a halt when presented with 10,000 items.

Understanding the concept and being able to apply it while writing code is therefore an important skill for any Java programmer.

Possible answers

There is really only one canonical answer, so the interesting thing is to see how the candidate approaches the problem.

The fact that max() should run in O(1) time is a clear hint that the maximum needs to be stored. Upon realizing this, the interview candidate might think they have solved the problem and start coding:

public class MaxStack<T extends Comparable<T>> extends Stack<T> {
    private T max;

    @Override
    public void push(T item) {
        if (max == null || item.compareTo(max) > 0) {
          max = item;
        }
        super.push(item);
    }

However, they’ll run into trouble with the pop() implementation, because the previous maximum is no longer available. If they try to solve this by scanning down the entire stack (an O(n) operation), point out that the Big-O complexity of existing methods needs to be preserved.

The correct approach, which stronger candidates should work out without diving into code first, and which moderately good candidates should still eventually come up with, is to use a second, internal stack to keep track of the maximum. A simple implementation of this idea:

public class MaxStack<T extends Comparable<T>> extends Stack<T> {
    private final Stack<T> maxs = new Stack<T>();

    private T max(T a, T b) {
        return a.compareTo(b) >= 0 ? a : b;
    }

    @Override
    public T push(T item) {
        if (maxs.empty()) {
            maxs.push(item);
        } else {
            maxs.push(max(currMax, item));
        }
        return super.push(item);
    }

    @Override
    public T pop() {
        maxs.pop();
        return super.pop();
    }

    public T max() {
        return maxs.peek();
    }
}

Follow-up questions

After this, you could have a discussion about what max() should return (or what it should throw) if the stack is empty, whether this implementation allows nulls on the stack (it doesn’t), and whether there is maybe a library implementation of max(Comparable<T>, Comparable<T>) (there isn’t).

Another few indicators to look out for, which are a sign that the interview candidate is a step ahead of the compiler:

  • Do they use inheritance correctly – in particular, do they enforce that T extends Comparable<T>?
  • Do they add @Override annotations?
10 Java Programming Questions to Ask on Interview (Explanations, Possible Answers, Following Questions)Click To Tweet

↑↑ Scroll up to the list of Java questions

[Question #6 – Changing Interfaces – Refactoring]

Assume you are working on a large system in which you’re in charge of some infrastructural aspect. You provide the other developers in the project a DataObject interface which they then implement to represent their data.

You are also in charge of a set of generic capabilities around DataObject such as reading/saving to a database, rendering in HTML, etc. A DataObject may have several methods, but for this question the only important one is toXML() which returns an XML representation of the data a DataObject contains.

In order to integrate with a third party service you need to add toJSON() method to DataObject. What is the problem with doing this, and how would you go about it while cause the minimum amount of disruption to the other programmers?

Why this programming question?

This java interview question is mostly for candidates who claim at least 2 years of experience working in a sizeable team. Working together with many people on a large codebase has its own unique set of difficulties, one of which is how to coordinate large-scale changes without stepping on each other’s toes.

Possible answers

The underlying problem is that adding a method to an interface in Java will result in compile errors for any class that implements the interface. So how do we make this change without breaking the build for everyone?

There is no clear-cut answer; different companies approach this differently. For example, Google works on a single huge code repository whereas Amazon breaks up its codebase into many smaller chunks. Neither approach is strictly “better”, as discussed in this great article.

Given this background, possible solutions include:

  • With a single, central repository, one could modify the interface and all its implementors in a single commit. This is only possible if the programmer making the change is at least somewhat aware of how to write all implementations, and knows whom to enlist for help with other teams’ code. Even then, the change is likely to be a bit painful, but it will be atomic.
  • If the code lives in different repositories, one could introduce a second version of the interface, which inherits from the first and adds the new method. When all implementors have switched to the second version, one would merge the first into the second and delete the first.
  • An alternative, perfectly valid but slightly “cheating” answer is to not change the interface at all; toJSON() could be a static method somewhere that calls toXML() and converts the result into JSON somehow.
  • Instead of implementing an interface, implementors could switch over to inheriting from an abstract class, AbstractDataObject, which implements the interface. The obvious drawback is that Java only has single inheritance, so this will not work if the implemetor already inherits from something else.
  • In Java 8, it is possible to add a “default” implementation of a method to an interface. This is a good solution, even if the default implementation merely throws something like UnsupportedOperationException, because at least it won’t break compilation. Of course, then one needs to take care to only call the method when one knows that it’s actually implemented (or gracefully handle failure).

Experienced candidates should be able to come up with all these approaches and reason about their tradeoffs.

Follow-up questions

If the interview candidate comes up with one of the above solutions without mentioning any drawbacks, ask whether this approach is perfect.

If using default interface methods, there is still a corner case where compilation might break: if the implementing class has a method with the same parameter types, but a different return value. You could ask the candidate what would happen in this case, and how they would solve it.

↑↑ Scroll up to the list of Java questions

[Question #7 – Algorithms]

Our movie streaming service logs the usage of the service to a text file for each subscribed user.

The logs contain the date that a movie was viewed, the name of the movie, the length of the movie and the number of minutes watched by the user and the genre of the movie. An example of a log file is shown below:

9/24/2016 The Magnificent Seven 133min 126min Action
9/30/2016 Miss Peregrine's Home for Peculiar Children 127min 100min Fantasy
11/5/2015 Trolls 92min 40min Fantasy
11/5/2015 Doctor Strange 115min 110min Fantasy
11/19/2016 Fantastic Beasts and Where to Find Them 133min 120min Fantasy
11/12/2016 Arrival 116min 20min SciFi

Write a program that will read this log file and produce the following information:

  1. An ordered list of movies watched by length
  2. The average percentage of the movie that this user watches. That is, if a movie is 60 minutes long and the user watched 30 minutes of the movie, the percentage watched is 50%. What is this average over all movies watched.
  3. The user’s favourite genre of movies. This is determined by first removing all movies where the user watched less than 50% of the movie then counting the movies in each genre that remains.

Why this Java interview question?

This is a test for the candidate’s understanding of streams in Java 8, which are great for performing such data processing with compact yet readable code.

When asking the question, emphasis should be put upon the data processing part, so the candidate doesn’t get bogged down in the details of parsing the text format.

Possible answers

To parse the log lines, a regular expression should do fine, but as noted, this is not the core part of the question. If you are short on time during the interview, tell the candidate to assume they already have a LogEntry class or similar, with suitable getters.

This regular expression string could be used to parse the input:

"^(\\d+)/(\\d+)/(\\d+) (.*) (\\d+)min (\\d+)min (\\S+)$"

Stream implementations of the requested output could look like this. To sort watched movies by length, assuming that logEntries is a List<LogEntry>:

logEntries
    .stream()
    .sorted((a, b) -> a.getLength() - b.getLength())
    .map(logEntry -> logEntry.toString())
    .collect(Collectors.joining("\n"))

To determine the average percentage watched:

logEntries
    .stream()
    .mapToDouble(logEntry -> (double) logEntry.getWatched() / logEntry.getLength())
    .average()
    .getAsDouble()

To determine their favorite genre:

logEntries
    .stream()
    .filter(logEntry -> logEntry.getWatched() * 2 >= logEntry.getLength())
    .collect(Collectors.groupingBy(LogEntry::getGenre, Collectors.counting()))
    .entrySet()
    .stream()
    .collect(Collectors.maxBy((a, b) -> (int) (a.getValue() - b.getValue())))
    .get()
    .getKey()

Of course, it’s unlikely that any interview candidate will write this out correctly without referring to documentation or IDE autocompletion.

You’re not looking for encyclopedic knowledge of the streams API, but rather for a general understanding of how it works and how it can be used to solve such problems.

Follow-up questions

You could drill into error handling. What happens if the input does not conform to the expected format? What if we try to compute the average of an empty list of numbers?

↑↑ Scroll up to the list of Java questions

[Question #8 – Array shuffle – Multithreading]

Given a T[], write a method shuffle that randomly reorders (shuffles) the array in place in an O(n) time complexity. You may use java.util.Random for this.

Assume that shuffling the entire array may be a very long process. Change the method so that multiple clients may consume already-shuffled cells from the array in a thread-safe way.

Why this question?

The first part is meant as a warm-up, asking for the standard Fisher-Yates shuffle algorithm. If the candidate doesn’t know it, and is unable to come up with it on the spot, they should be gently nudged towards it until they write it down as it’s an important foundation for the second part of the question.

The second part is not much about algorithms at all, but rather about concurrency. As mentioned before, concurrency is tricky to get right, and this is also one of the advanced java interview questions. But selecting the right concurrency primitives from the Java standard library goes a long way towards a correct implementation. Experienced candidates should be able to come up with a correct solution here.

Possible answers

The shuffle itself is straightforward:

public static void shuffle(T[] array) {
  Random random = new Random();
  for (int i = 0; i < array.length; i++) {
    int j = i + random.int(array.length - i);
    T tmp = array[i];
    array[i] = array[j];
    array[j] = tmp;
  }
}

To make it possible to consume values concurrently, an inexperienced candidate might start making methods synchronized. This will not work, because as soon as the shuffle() method is synchronized, no other methods can be called concurrently anymore.

Instead, we need to keep track of which values have already been shuffled, and which have already been handed out to consumers. We can track the next index to be consumed using an AtomicInteger, but this index might not have been shuffled yet.

To track how many values have been shuffled, we can use a Semaphore. Each time a value is shuffled, the semaphore is released, allowing one value to be consumed:

public class ArrayShuffler<T> {
    private T[] arr;
    private AtomicInteger index;
    private Semaphore semaphore;

    public ArrayShuffler(T[] arr) {
        this.arr = arr;
        index = new AtomicInteger(0);
        semaphore = new Semaphore(0);
    }

    public void startShuffling() {
        Random random = new Random();
        for (int source = 0; source < arr.length; ++source) {
            int target = 
                random.nextInt(arr.length - source) + source;
            T temp = arr;
            arr = arr[target];
            arr[target] = temp;
            semaphore.release();
        }
    }

    public T getNext() throws InterruptedException {
        semaphore.acquire();
        int ind = index.getAndIncrement();
        return arr[ind];
    }
}

Multiple threads may acquire the semaphore before proceeding to actually get the value; you can ask whether this is a problem. The candidate should point out that it’s fine, because AtomicInteger.getAndIncrement() will hand out each index exactly once.

Follow-up questions

Ask about the exact behavior of the Random class. Is it a good idea to create a new one for every shuffle() call?

You can also ask about testing strategies. Senior candidates should point out that the Random instance will need to be injected and mocked, in order to have reproducible tests. They might also consider doing some kind of statistical test to ensure that each possible ordering is equally likely (or at least likely to be equally likely).

10 Java Programming Questions to Ask on Interview (Explanations, Possible Answers, Following Questions)Click To Tweet

↑↑ Scroll up to the list of Java questions

[Question #9 – Palindrome – Algorithms]

Write Java methods that checks if a String is a palindrome (i.e. is equal to itself when reversed).

Check if a string has any permutation that is a palindrome. In other words, is there any way to rearrange the characters in the string so that a palindrome is produced?

Why this Java coding question?

The first part, checking whether a string is a palindrome, is conceptually trivial but can already reveal how much practical coding experience the candidate has. The second part is the most interesting, because obvious solution of checking all possible permutations is horribly slow.

Possible answers

To check if a string is a palindrome:

public static boolean isPalindrome(String string) {
  int n = string.length();
  for (int i = 0; i < n / 2; i++) {
    if (string.charAt(i) != string.charAt(n - 1 - i)) {
      return false;
    }
  }
  return true;
}

Things to watch out for:

  • Does it work correctly for the empty string?
  • Does the code only loop to halfway the string (rounded down), and not all the way to the end?
  • Are there no off-by-one errors?

And for more experienced interview candidates (5 years experience):

  • Are methods and variables named descriptively and succinctly?
  • Are edge conditions (e.g. null input) checked for?

If they don’t write any of this out, they should at least mention it verbally.

For the second part, it is tempting to start enumerating all possible permutations of the string, and passing each in turn to the above method. If the candidate suggests this approach, ask them about performance, both in Big-O terms and in practical terms. How long would you expect it to take for a string of length 10? 20? 30?

While it’s interesting in its own right to see the candidate develop a permutation generator, it’s not the focus of this coding question, and they should be nudged to find a more efficient solution.

The proper solution to the second part involves the realization that we can make a palindrome if we have an even number of each character, and possibly one extra character to put in the middle (if the number of characters is odd).

Using streams, a solution could look as follows:

public static boolean hasPalindromePermutation(String str) {
  return str.chars()
            .boxed()
            .collect(Collectors.groupingBy(
                Function.identity(),
                Collectors.counting()))
            .entrySet()
            .stream()
            .filter(e -> e.getValue() % 2 == 1)
            .count() <= 1;
    }

Or in “classic” Java:

public static boolean hasPalindromePermutation(String string) {
  Map<Character, Integer> counts = new HashMap<>();
  for (Character c : string) {
    if (!counts.containsKey(c)) {
      counts.put(c, 0);
    }
    counts.put(c, counts.get(c) + 1);
  }

  int oddCount = 0;
  for (Integer count : counts.values()) {
    if (count % 2 == 1) {
      oddCount++;
      if (oddCount > 1) {
        return false;
      }
    }
  }
}

Follow-up questions

As usual, asking for unit test cases provides a good starting point for further discussion. In this particular case, I’d like to see the following classes of input tested:

  • null,
  • the empty string,
  • a single character,
  • two identical characters,
  • two different characters,
  • three characters, of which two are identical, in all possible orders,
  • three different characters,
  • a long string (30+ characters) for which a palindrome permutation exists,
  • a long string for which a palindrome permutation does not exist.

↑↑ Scroll up to the list of Java questions

[Question #10 – Rectangle – Algorithms]

Write a method named “contains” that accepts a point (x, y) and returns true if the point occurs within a rectangle, false otherwise. Do not use the features of the java.awt.geom package.

Why this Java question?

The question sounds trivial, and the simplest possible implementations are indeed quite simple.

The interesting thing about this question is that it’s very vague, and requires that the candidate asks questions of their own to clarify the requirements before coding.

Possible answers

It is obvious that such a contains() method should be a method on a Rectangle class. That still leaves many questions to be answered:

  • How do we represent coordinates? Integers, longs, floats, doubles? Each of these is appropriate in a different situation, and the candidate should be able to reason about the tradeoffs.
  • What does our coordinate system look like? Y-up or y-down? Does it matter?
  • How do we represent the rectangle? As a corner point and a size? Center point and size? Two corner points? Ranges of x and y coordinates?
  • When does a point count as “contained”? What if it’s exactly on the boundary?
  • Is the rectangle axis-aligned, or can it be rotated?

For example, if we use doubles, the rectangle is axis-aligned and represented by a corner and two lengths, and points on the boundary are excluded, this would be a valid solution:

public boolean contains(double x, double y){
  return x > this.x && x < this.x + this.width && y > this.y && y < this.y + this.height;
}

Follow-up questions

If simplifying assumptions were made (e.g. an axis-aligned rectangle), you can remove these assumptions one by one and ask the interview candidate to adapt the code to the new circumstances. In particular, testing whether a point is inside a rotated rectangle is considerably harder.

As usual, asking for test cases is a good way to see whether the candidate has considered all possible inputs and failure modes.

Keep in mind that geometry and vector algebra are not every programmer’s strong suit, and are only needed in particular jobs, like game development or scientific modelling. If it’s not needed for the position you’re hiring for, or the candidate’s CV does not indicate any experience in this area, you should probably go light on the math.

↑↑ Scroll up to the list of Java questions

Java Test

[Java Interview Test]

Don’t have the time to interview every single candidate in person?

We’ve got you covered there, too. We also provide a 20-question multiple-choice Java quiz: all you have to do is send each candidate their own unique link.
When a candidate finishes a test, you’ll be automatically notified by email with a report attached!

Better yet, why not mix and match? You can use our multiple-choice quiz to filter out incompetent candidates, then invite most skilled candidates for a face-to-face interview using our questions.

You can weed out up to 70% of candidates without ever seeing them in person – spend time on promising candidates only!

10 Java Programming Questions to Ask on Interview (Explanations, Possible Answers, Following Questions)Click To Tweet

↑↑ Scroll up to the list of Java questions

Authors

These Java interview questions have been created by the team of Java professionals:

↑↑ Scroll up to the list of Java questions

Like the article? Share it please!

It will REALLY help us to make more content here.

3 Shares

  15 Comments

  1. Etienne Houle   •  

    Great article! But the given answer for the MaxStack question is bogus. pop() assumes that items were pushed in increasing order. I believe there is no way to keep the O(1) complexity. Thanks!

  2. c   •  

    s

  3. pranit patil   •  

    Good work and super effort taken here to combine the questions and answers of java that can be ask in interview.
    As java fresher, one should follow this list of questions and answer to prepare for interview. Thanks a lot for sharing.

    • Tests4Geeks   •     Author

      Thank you, Pranit!

  4. rsingh   •  

    Good write up on java interview questions

    • Tests4Geeks   •     Author

      Thanks

  5. Priya   •  

    All the above explained questions are really important for me. Many thanks for sharing this , keep blogging.

  6. Folefac   •  

    Cool, keep up

  7. Remy   •  

    Hello thank you for these, its really interesting, I am enjoying trying to solve those questions.

    Regarding Exercice5, as Etienne Houle said, I have the impression the solution given is wrong.
    If you run your test, you will see it’s not gonna work as soon as you unstack a few values. (btw: where the currMax variable comes from?)

    However, instead of implementing a sub Stack pile, I have implemented a TreeMap.
    Always sorted and every manipulation give a O(logn).

    private final TreeMap treeIndex = new TreeMap();

    T would be the Item, and Integer a counter. Of course, the Item must implements Comparable and we must override compareTo(T) according to our needs. (I made my example with Book, playing with AuthorName, Editor, etc…)
    .
    Hence TreeMap will be able to order them automatically, and you can get the max by calling
    lastKey();

    In case two items are equal, I increment the counter in the TreeMap. When poping, I decrement.

    Now I won’t be able to say if my solution is O(1) or O(logn) as every manipulation with the treeSet is Log(N) except to get the lastKey(O(1)).

    Rémy

  8. Remy   •  

    Sorry there were a bug with previous post, because of special character.

    The treeMap I declared use the two elements: T and Integer.

  9. Pingback: sourcing agent in china

  10. Pingback: construction videos

  11. Shalmali Bapat   •  

    Interviews always create some tensed situation and to make you feel easy about them you have provided some nice and important java programming interview questions which will be very useful for people who are preparing for interviews. Going to save this for sure.

  12. Chloe Jeffey   •  

    For my job interview preparation I used:
    “Cracking the Coding Interview” by Gayle Laakmann McDowell
    “TOP 30 Java Interview Coding Tasks” by Matthew Urban
    “Elements of Programming Interviews in Java” by Adnan Aziz, Tsung-Hsien Lee, Amit Prakash
    “Coding Interview Questions” by Narasimha Karumanchi

    • Michael Bubbles   •  

      I would recommend the “TOP 30 Java Interview Coding Tasks” because it is very popular between recruiters in IT companies

Leave a Reply

Your email address will not be published. Required fields are marked *