Skip to main content

Nagarro Java Developer Interview Questions & Answers

This guide compiles real interview questions and detailed answers from a Java Developer interview at Nagarro. The candidate had 4 years of experience, and the interview process consisted of an online assessment followed by two technical rounds.


Round 1: Core Java & Spring Boot

Q: Explain some concepts introduced in Java 8.

A: Java 8 introduced several major features to modernize the language:

  • Lambda Expressions: Allowed for cleaner, more concise functional programming.
  • Stream API: Provided a powerful way to process collections of data declaratively.
  • Optional: Introduced to handle potential null values gracefully and avoid NullPointerException.
  • Default Methods in Interfaces: Allowed interfaces to have method implementations without breaking existing implementing classes.
  • Date & Time API (java.time): A new, thread-safe, and comprehensive API for handling dates and times.

Q: What is the difference between Lambda Expressions and Method References? Provide examples.

A: * Lambda Expression: An anonymous function used to implement a functional interface. It provides a concise way to pass behavior as a parameter. Example: list.forEach(item -> System.out.println(item));

  • Method Reference: A shorthand syntax that points directly to an existing method. It is used when a Lambda expression does nothing but call an existing method. Example: list.forEach(System.out::println);

Q: What is the difference between map() and mapToObject() in Streams?

A: * map(): Transforms elements within an object stream and returns another object stream (e.g., transforming a Stream<String> to a Stream<Integer>).

  • mapToObject(): Used specifically on primitive streams (like IntStream, LongStream, DoubleStream) to convert the primitive values into a stream of objects (e.g., converting an IntStream to a Stream<Integer>).

Q: What is the use of the @SpringBootApplication annotation?

A: The @SpringBootApplication annotation is a convenience annotation that combines three key annotations:

  1. @Configuration: Tags the class as a source of bean definitions.
  2. @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  3. @ComponentScan: Tells Spring to look for other components, configurations, and services in the current package and its sub-packages. It drastically reduces boilerplate code and simplifies the application setup.

Q: What is the difference between the IoC Container and ApplicationContext?

A: * IoC Container (BeanFactory): The basic container that manages object creation, configuration, and dependency injection.

  • ApplicationContext: An advanced sub-interface of BeanFactory. It includes all the functionality of the IoC container but adds enterprise-specific features like event propagation, internationalization (i18n), and AOP integration. ApplicationContext is used in almost all modern Spring applications.

Q: Explain the implementation of a JWT (JSON Web Token) and the significance of its signature.

A: * Implementation: A JWT consists of three parts: Header, Payload, and Signature. The server generates the token, signs it using a secret key, and sends it to the client. The client sends it back on future requests, and the server verifies it for stateless authentication.

  • Significance of Signature: The signature ensures the token's integrity and authenticity. It is generated using the Header, Payload, and the server's secret. If a user tries to alter the payload (e.g., changing their role from "user" to "admin"), the signature validation will fail, and the server will reject the token as tampered or invalid.

Round 1: Microservices, Databases & Deployment

Q: How does communication happen in Microservices?

A: Microservices communicate using:

  • Synchronous Communication: Using REST APIs or gRPC where the client waits for the server to respond.
  • Asynchronous Communication: Using Message Brokers (like Kafka, RabbitMQ) where services publish and subscribe to events without waiting for an immediate response.

Q: Can a REST API be used for asynchronous communication? If yes, how?

A: Yes, REST APIs can support asynchronous communication using techniques like:

  • Webhooks: The client provides a callback URL, and the server pushes the response to it when the task is done.
  • Long Polling: The client holds the connection open until the server has new data.
  • 202 Accepted Pattern: The server immediately returns an HTTP 202 (Accepted) with a Job ID, and the client polls a status endpoint later to get the final result.

Q: Explain the Saga Design Pattern.

A: The Saga pattern manages distributed transactions across multiple microservices. Instead of a single massive database lock (which doesn't work across different databases), Saga breaks the transaction into multiple local steps. If any step fails, the pattern triggers compensating transactions to undo the changes made by the previous steps, ensuring eventual data consistency.

Q: How do you handle Cascade Failures in Microservices?

A: Cascade failures occur when one failing service brings down dependent services. To handle this, we use:

  • Circuit Breakers (e.g., Resilience4j): Stops sending requests to a completely failing service immediately.
  • Timeouts: Prevents services from waiting indefinitely for a response.
  • Retries: Retrying transient failures with a set limit and exponential backoff.
  • Fallbacks: Providing a default response or alternative logic when a service is down.

Q: What is the difference between Clustered and Non-Clustered Indexes?

A: * Clustered Index: Physically sorts and stores the data rows in the table based on the key. There can be only one clustered index per table.

  • Non-Clustered Index: Creates a separate structure (like a lookup table) that holds the index key and pointers to the actual physical data. A table can have multiple non-clustered indexes.

Q: What is the difference between a View and a Table?

A: A Table physically stores the data on the disk. A View is a virtual table created by a query joining one or more tables. Views do not store data themselves; they dynamically fetch data when queried, making complex queries simpler and improving security by restricting data access.

Q: What are the different types of deployments?

A: * Rolling Deployment: Gradually replaces old instances with new ones, ensuring zero downtime.

  • Blue-Green Deployment: Maintains two identical production environments. Traffic is switched instantly from the old (Blue) to the new (Green) environment.
  • Canary Deployment: Releases the new version to a very small subset of users first to monitor for issues before rolling it out to everyone.
  • Shadow Deployment: Production traffic is mirrored to the new version for testing without affecting the actual end-users.

Round 2: Data Structures & Advanced Java

Q: Write a Java code to reverse a Singly Linked List.

(Standard algorithmic coding question evaluating pointer manipulation).

Q: Given a Linked List where each node represents a character, write code to prove it is a palindrome.

(Standard algorithmic coding question, usually solved by finding the middle, reversing the second half, and comparing the two halves).

Q: Explain the Singleton design pattern and give a thread-safe implementation.

A: The Singleton pattern ensures that a class has only one instance globally. Thread-Safe Implementation: The best approach is using Double-Checked Locking.

public class Singleton {
private static volatile Singleton instance;
private Singleton() {} // Private constructor

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

Q: Explain the difference between HashMap, LinkedHashMap, and TreeMap with examples.

A: * HashMap: Stores data in a random order based on hash codes. Best for fast $O(1)$ access.

  • LinkedHashMap: Maintains the exact order of insertion using a doubly-linked list. Use when order matters.
  • TreeMap: Stores keys in a naturally sorted order (or via a custom Comparator) using a Red-Black tree. Use when sorting is required.

Q: Explain the difference between ConcurrentHashMap and Collections.synchronizedMap().

A: * synchronizedMap(): Locks the entire map for every single read or write operation. This severely bottlenecks performance in a multi-threaded environment.

  • ConcurrentHashMap: Uses "fine-grained locking" (or segment locking). It locks only the specific bucket being updated, allowing multiple threads to read and write to different parts of the map safely and simultaneously, offering vastly superior performance.

Q: How do you avoid Deadlocks in Java?

A: * Lock Ordering: Ensure all threads acquire multiple locks in the exact same predefined order.

  • Lock Timeouts: Use tryLock(time) from ReentrantLock so a thread backs off if it cannot acquire the lock.
  • Avoid Nested Locks: Do not hold a lock while attempting to acquire another if possible.
  • Detect Circular Dependencies: Carefully review thread logic to prevent threads from waiting on each other indefinitely.

Round 2: Database Optimization & Spring Web

Q: Explain Unique Key, Primary Key, and Foreign Key.

A: * Unique Key: Ensures all values in a column are entirely distinct. Allows a single null value.

  • Primary Key: Uniquely identifies each record in a table. It cannot be null and there can only be one per table.
  • Foreign Key: Links two tables together. It enforces referential integrity by ensuring the value in one table matches a Primary Key in another table.

Q: Describe your experience with query tuning. What tools and techniques do you use to resolve performance issues?

A: * Techniques: Analyzing execution plans, avoiding SELECT *, avoiding heavy subqueries, creating proper indexes, and ensuring table joins are optimized.

  • Tools: Using commands like EXPLAIN ANALYZE to read execution plans, and utilizing UI tools like MySQL Workbench or pgAdmin to identify slow queries and bottlenecks.

Q: What is the difference between @PathVariable and @RequestParam? Give an example.

A: * @PathVariable: Extracts values directly from the URI path. Example: /users/{id} -> /users/123. Used to identify a specific resource.

  • @RequestParam: Extracts values from the query string of the URL. Example: /users?id=123. Used to filter, sort, or pass optional data.

Q: What is the use of the @ResponseBody annotation?

A: The @ResponseBody annotation in Spring tells the controller that the returned object should be automatically serialized (usually to JSON or XML) and written directly into the HTTP response body, bypassing the traditional view resolution (HTML/JSP) step. It is fundamental when building REST APIs.