Interview Q&A / Java
Java interview questions
Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.
Prefer rapid revision? Practise Java short interview answers →
- What is the difference between ArrayList and LinkedList in Java?
ArrayList is backed by a resizable array, so reading by index is O(1) but inserting or removing in the middle shifts elements. LinkedList is a doubly linked list, so adding or removing at the ends is O(1) but reaching an index means walking the nodes. In practice ArrayList is the better default almost every time.
IntermediateIn-depth3 min read - How does HashMap work internally in Java?
HashMap stores entries in an array of buckets. It hashes the key, picks a bucket from that hash, and stores the entry there; keys that land in the same bucket form a linked list, which since Java 8 turns into a red-black tree when it gets long. When the map is 75% full it doubles the array and redistributes entries.
AdvancedIn-depth4 min read - How does a HashSet avoid storing duplicates in Java?
A HashSet is backed by a HashMap and stores each element as a key. add() uses hashCode to find the bucket and equals to check for an existing equal element; if one exists the add returns false and nothing is stored.
BeginnerQuick answer1 min read - What causes ConcurrentModificationException and how do you avoid it?
It is thrown when a collection is structurally modified while you iterate over it, for example calling list.remove() inside a for-each loop. Remove through the Iterator, use removeIf, or iterate over a copy.
BeginnerQuick answer1 min read - What is the difference between Comparable and Comparator in Java?
Comparable defines one natural order inside the class itself through compareTo. Comparator is a separate object with a compare method, so you can define many different orderings without changing the class.
BeginnerQuick answer1 min read - What is the difference between HashMap, LinkedHashMap and TreeMap?
HashMap has no guaranteed order and gives O(1) operations. LinkedHashMap keeps insertion order, also in O(1). TreeMap keeps keys sorted and costs O(log n). Pick by the order you need.
BeginnerQuick answer1 min read - Write a Java method to remove duplicates from a list while keeping the original order.
Put the elements into a LinkedHashSet, which drops repeats and remembers first-seen order, then copy it back into a list. It takes O(n) time and O(n) space.
BeginnerQuick answerCoding1 min read - How does PriorityQueue order its elements in Java?
PriorityQueue is a binary min-heap, so poll() always returns the smallest element by natural order or a Comparator. Add and poll cost O(log n), peek is O(1), and iterating it does not give sorted order.
IntermediateQuick answer1 min read - What is a TreeMap and how does it keep its keys sorted?
TreeMap stores entries in a red-black tree, a self-balancing binary search tree, ordered by the keys natural order or a supplied Comparator. get, put and remove take O(log n), and it offers range methods like firstKey and floorKey.
IntermediateQuick answer1 min read - Write Java code to sort employees by salary (highest first) and then by name.
Build a Comparator with Comparator.comparingDouble(Employee::salary).reversed().thenComparing(Employee::name) and pass it to list.sort. Sorting costs O(n log n) time.
IntermediateQuick answerCoding1 min read