2026 - Backend Interview Questions
1. Core Java & OOP Write an immutable class in Java Comparable vs Comparator with examples What is a Functional Interface? Can it have default and static methods? Explain try-with-resources. What happens if both try and close() throw exceptions? 2. JVM, Memory & Performance JVM memory management Your Java application has high GC pauses in production — what would you investigate? How would you…
1. Core Java & OOP
An immutable class in Java is one whose state cannot be modified after it is created. This is useful in scenarios where thread safety is important. Comparable is used to sort objects of a class by providing their natural ordering, while Comparator is used to sort objects based on custom criteria. Both can be used to define the order of elements in a collection.
Functional Interface is an interface that has exactly one abstract method. Java 8 introduced this concept, enabling the use of lambda expressions. Such interfaces can have default and static methods, which provide default implementations or utility methods, respectively.
Try-with-resources is a Java feature that automatically closes resources (like file streams or database connections) when they are no longer needed. If both the try block and close() method throw exceptions, the exceptions from the try block are propagated first, followed by the exception from close().
2. JVM, Memory & Performance
JVM memory management involves heap and non-heap memory. The heap is where objects are allocated, while non-heap memory includes the method area and string intern pool. In production, high GC pauses can be due to large object allocation, excessive memory usage, or frequent garbage collection cycles.
To identify a memory leak, you can use tools like VisualVM or JProfiler to monitor heap usage over time. Look for patterns where memory usage keeps increasing without any release, indicating a potential leak.
3. Concurrency & Multithreading
Producer-Consumer problem is a classic example of concurrent programming where one thread (producer) adds items to a buffer and another thread (consumer) removes them.
Deadlock is a situation where two or more threads are blocked forever, each waiting for the other to release a resource. For example, Thread A holds resource 1 and waits for resource 2, while Thread B holds resource 2 and waits for resource 1.
A thread-safe Singleton using double-checked locking ensures that only one instance of a class is created, even in a multi-threaded environment. It checks if an instance exists (lock-free), creates it if not, and then double-checks to ensure only one instance is created.
4. Collections & Internals
HashMap is a key-value data structure that stores elements in buckets using a hashing function. Collisions are handled using chaining, where each bucket contains a linked list of entries that hash to the same index.
ConcurrentHashMap is a thread-safe variant of HashMap, allowing concurrent reads and exclusive writes. It uses segment locks to divide the map into segments, allowing multiple threads to access different segments simultaneously, reducing contention.
5. Java 8 & Streams
Java 8 Stream API is a collection pipeline that processes elements sequentially or in parallel. It provides a rich set of operations like filter, map, sort, and collect to process collections. map() transforms elements, while flatMap() flattens a stream of streams or collections into a single stream.
A Java 8 program using flatMap() to flatten a nested list would involve mapping each nested list to its elements and flattening the result. Sorting an Employee list using Comparator & Streams involves defining a comparator that compares salaries and then sorting the list using that comparator. Finding Top N highest salaries using Streams involves using the limit() function after sorting the list by salary.
6. Data Structures — Arrays & Lists
Finding duplicate elements in an array involves comparing each element with every other element. Merging two sorted arrays can be done in O(n) time by iterating through both arrays simultaneously and adding the smaller element to the result.
7. Data Structures — Strings
Finding the first and second non-repeating characters in a string involves iterating through the string and counting character occurrences. Reversing a string without using inbuilt methods can be done by swapping characters from the start and end of the string moving towards the center. Finding the length of the longest substring containing at most K distinct characters can be optimized using a sliding window approach with a HashMap to keep track of character frequencies. Removing duplicates from a string can be done by using a HashSet to keep track of seen characters.
8. Data Structures — Stacks, Caching & Classic Problems
Balanced parentheses problem using Stack involves pushing opening brackets onto a stack and popping them when a closing bracket is encountered. If the stack is empty at the end, the parentheses are balanced. Implementing an LRU Cache involves using a HashMap to store key-value pairs and a doubly-linked list to keep track of the least recently used items.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.