Senior Java Software Engineer Interview Notes – Part 1

This is the 1st post regarding my interview experience for Senior Java Software Engineer roles in the UK. Note that these are NOT copy-paste interview questions that I found on the internet but the actual ones asked during interviews.

Collections API

Which data structure is used when you have lots of add and remote operations? What is time complexity?


Why LinkedList would be a better option than ArrayList when you have lots of add and remote operations?
The insertion, addition and removal operations of an item are faster in a LinkedList because there is no need to resize an array or update the index when an element is added to some arbitrary position inside the collection, only references in surrounding elements will change.

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (O(n) time). The constant factor is low compared to that for the LinkedList implementation.

Let’s say you have books. If you want to get all books sorted by name, which data structure would be faster?

If you want to sort objects by their member variable, such as a name, you would need to use either Comparable or Comparator interface.

What is the time complexity of add, remote, and search operations for data structures LinkedList, ArrayList, HashMap, TreeMap, HashSet…?

What is the cost of searching in TreeSet?


This implementation provides guaranteed log(n) time cost for the basic operations (addremove and contains).

How hashmap works internally?

HashMap maintains an array of buckets, which contains a key, value, and hash. Each bucket uses a LinkedList to store objects and can have multiple key-value pairs. When we store or retrieve any key-value pair, HashMap calculates the index of the bucket for each and every operation. The index value is used to find the bucket location in the array.

How put(K key, V value) method works internally in HashMap?

When we pass key and value object to put() method, the HashMap implementation calls the hashCode method on the key object. The value retrieved from the hashCode determines the location of the bucket in the array. It is important to know that HashMap in Java stores both key and value object as Map.Entry in a bucket. Read more:

    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

How get(Object key) method works internally in HashMap?

In HashMap, get(Object key) calls hashCode() on the key object and uses the returned hash value to find a bucket location, where keys and values are stored as an Entry object. The hashValue is used to find the bucket location at which Entry object is stored. Entry object stores in the bucket as (hash, key, value, bucket index). Then, the value object is returned.

   public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);

What is the contract between equals and hashcode?

  1. If two objects are equal with respect to the equals() method, then they must have the same hash code. In other words, if x.equals(y),then x.hashCode() == y.hashCode().
  2. If two objects have the same hash code, they may or may not be equal. Therefore, if x.hashCode() == y.hashCode(), then x == y or x != y.

What are the pros and cons of using immutable classes?

There is a good discussion on Quora about this.


What is deadlock?

What is a race condition?

You have 2 files each takes 10 seconds to write. How can you make it faster than 20 seconds to write those files? (multithreading)

How can you control the use of these files if you use multiple threads?


What is the difference between queue and stack?

Method overloading vs overwriting?

When are the overloading and overwriting evaluated (compile time vs runtime)?

What is dependency injection and what is the advantage of using it?

Checked vs unchecked exceptions

Checked exceptions are exceptional conditions that a well-written application should anticipate and recover from, such as Checked exceptions are subject to the Catch or Specify Requirement. All exceptions are checked exceptions, except for those indicated by ErrorRuntimeException, and their subclasses.

Unchecked exceptions, also known as runtime exceptions, are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as NullPointerException.

Leave a Reply

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