Skip to content
Practice

Java backend interview questions

20 scenario questions of the kind asked in backend interviews. Answer each out loud first, then open the short answer. The links lead to the lesson that shows the mechanism, with output from a real JVM or Spring Boot application.

Core Java

5 questions

  1. A HashMap holds a million entries and has become slow. What are the likely causes, and how do you fix each?

    Show a short answer

    Lookups are fast only while keys spread across the buckets. Suspect a weak hashCode first, which puts many keys in few buckets. Then check repeated resizing, the memory cost of boxed entries, and a map that never removes anything.

  2. The code uses synchronized everywhere and the application is slow. What is going on?

    Show a short answer

    One lock turns many threads into a queue: only one runs the guarded code at a time, so more threads add waiting, not throughput. A thread dump shows many threads BLOCKED on the same monitor. Shorten the guarded section, split the lock, or use a concurrent collection or an atomic.

  3. Why does double-checked locking in a singleton need volatile?

    Show a short answer

    Without volatile, a second thread can read a non-null reference before the writes made by the constructor are visible to it, and use a half-built object. volatile orders the write of the reference before every later read of it.

  4. A ThreadLocal is causing a memory leak. How does that happen?

    Show a short answer

    Pool threads are never discarded, so a value set on one and not removed stays reachable from that thread for the life of the process. Call remove() in a finally block when the work is done.

  5. wait and sleep both pause a thread. Where can each one leave threads stuck?

    Show a short answer

    sleep keeps every lock the thread holds, so sleeping inside a synchronized block stalls every thread that needs it. wait releases the one monitor it was called on. It hangs when the notification was already sent, or when the thread still holds an outer lock that the notifier needs.

JVM and memory

3 questions

  1. A service ran for 30 days, then died with OutOfMemoryError. No code changed. What happened?

    Show a short answer

    A slow leak. Something is added to a collection, cache or ThreadLocal on each request and never removed, so the heap left after each full collection creeps up for weeks. Compare two class histograms taken some time apart, then take a heap dump and follow the path to the GC root.

  2. Garbage collection runs every 30 seconds and pauses the application for 2 seconds each time. How do you approach it?

    Show a short answer

    Read the GC log before changing a flag: which kind of collection pauses, and how much is still live afterwards. Then decide between giving the heap more room, allocating less, and moving to a collector built for short pauses.

  3. When would you choose G1 over ZGC in production?

    Show a short answer

    Choose from the pause the service can tolerate. G1 is the default: it stops the application to copy live objects, aiming at a pause goal of 200 ms. ZGC keeps pauses under a millisecond and pays for it with heap and CPU, so stay with G1 while a healthy G1 pauses briefly enough.

Spring Boot

4 questions

  1. @Transactional on a private method silently has no effect. Why?

    Show a short answer

    The transaction is started by a proxy that stands in front of the bean. A private method cannot be intercepted, and a call from inside the same class never reaches the proxy. The method runs with no transaction and no warning.

  2. Two beans of the same type exist. How does Spring decide which one to inject?

    Show a short answer

    It narrows the candidates by @Qualifier, then @Primary, then the name of the parameter or field. If more than one is still left, the application fails to start with NoUniqueBeanDefinitionException. @Primary wins over a matching name.

  3. An @Async method throws and nothing handles it. What happens to the exception?

    Show a short answer

    The caller has already returned, so it cannot catch it. For a void method the exception goes to the AsyncUncaughtExceptionHandler, which by default only logs it. For a method that returns a future it is stored in the future, and lost if nobody reads it.

  4. Startup takes 45 seconds. How do you find out why and bring it down?

    Show a short answer

    Record the startup steps and sort them by duration before changing anything. Usually a few beans do slow work, such as remote calls, while the context starts. Move that work off the startup path. Lazy initialisation only moves the cost to the first requests.

Database and JPA

3 questions

  1. LazyInitializationException appears in production but not in development. Why?

    Show a short answer

    A lazy association can only load while its session is open. Spring Boot keeps a session open for the whole web request by default, so the read works there. It fails when production switches that off, or runs the same code in a job, a listener or an async thread.

  2. A JPA query is fast with 100 rows and takes 45 seconds with a million. What is wrong?

    Show a short answer

    Most often one query per row of a list, or a query the database cannot answer from an index. Neither shows at 100 rows. Count the SQL statements one request sends, then read the plan of the slow one.

  3. Two transactions update the same row and one silently overwrites the other. How do you prevent it?

    Show a short answer

    Make the write conditional on the row being unchanged since it was read, usually with a version column. The second writer then fails and can retry with fresh data, where before it would have overwritten the first.

System design and production

5 questions

  1. A payment API receives the same request twice because of a network retry. How do you prevent a double charge?

    Show a short answer

    The client sends a unique idempotency key with the request. The server records the key together with the result, and answers any repeat of that key with the stored result without charging again.

  2. Service A calls service B. B is down, A keeps retrying, and B cannot recover. Which pattern addresses this?

    Show a short answer

    A circuit breaker: after enough failures A stops calling B for a while and fails fast, then lets a few test calls through. Retries also need a limit, because retries at several layers multiply into more load than B could ever serve.

  3. The Redis cache is working, yet database load stays at 100%. What is going wrong?

    Show a short answer

    A high hit ratio says nothing about how many requests still miss. Look for lookups of things that do not exist, which are never cached, and for many keys expiring at the same moment.

  4. Response time is 20 ms normally and 8 seconds under load, with no errors in the logs. Where do you start?

    Show a short answer

    No errors means requests are waiting, not failing. Look for the queue: a thread pool or connection pool with no free slot, or a dependency running close to its limit, where waiting time rises steeply.

  5. A feature is deployed on Friday evening. By Monday morning everything is slow, and no code has changed since. What happened?

    Show a short answer

    The Friday change is still the last change. Its cost grows with something that built up over the weekend, such as rows in a new table. Quiet weekend traffic hid the rising cost, and Monday's peak exposed it.

Looking for system design questions by topic? See the interview question round-up.