LTIMindtree Java Developer Interview Questions & Answers
This guide covers real-world technical interview questions asked during an LTIMindtree Java Developer interview. The role was targeted at candidates with 2 to 7 years of experience, and the technical round covered Core Java, Spring Boot, Databases, and specific coding challenges.
1. Core Java & Java 8 Features
Q: Give a scenario where you would use a traditional Loop and where you would use the Stream API.
A:
- Traditional Loop: Better suited for small datasets or performance-critical sections where the slight overhead of stream creation is unwanted.
- Stream API: Better suited for large datasets, parallel processing, and complex data transformations (like filtering, mapping, and grouping), as it makes the code much more readable and declarative.
Q: Why do we have a static entry point (main method) in a Java program?
A: The static keyword indicates that a method belongs to the class itself rather than an instance of the class. We make the main method static so the Java Virtual Machine (JVM) can invoke it directly to start the program without needing to instantiate an object of the class first.
Q: What is the difference between ArrayList and LinkedList? Give a scenario for when to use each.
A:
ArrayList: A dynamic array. It is great for fast, index-based access ($O(1)$) but slow when adding or removing elements in the middle because elements must be shifted.- Scenario: Use an
ArrayListwhen you need quick access to items, such as displaying a catalog of products in a search result.
- Scenario: Use an
LinkedList: Elements are connected via nodes. It allows fast additions and deletions ($O(1)$) because no shifting is required, but accessing an element is slow ($O(n)$) because it must traverse the list.- Scenario: Use a
LinkedListwhen you frequently add or remove items, such as managing a queue of active background tasks.
- Scenario: Use a
Q: Explain the internal working of HashMap.
A: A HashMap stores key-value pairs in an array where each index acts as a bucket. It uses a hash function on the key to determine which bucket the entry should go into, allowing for highly efficient data retrieval. If two different keys end up in the exact same bucket, a hash collision occurs. The HashMap manages these collisions by storing the colliding entries in a linked list (or a balanced Red-Black tree in newer Java versions) within that specific bucket.
Q: What is a Singleton class, how do you create one, and where would you use it in a project?
A: A Singleton class is a design pattern that ensures a class can have only exactly one instance at any time.
- Creation:
- Make the constructor
private. - Create a
private staticinstance variable of the class. - Provide a
public staticmethod (e.g.,getInstance()) to return that instance.
- Make the constructor
- Project Scenario: Used for a centralized
Loggerclass that manages how messages are written to log files throughout the entire application.
Q: What are immutable classes and how do you create them?
A: Immutable classes create objects whose state cannot be modified after they are created (e.g., String).
- Creation: Declare the class as
final(so it cannot be extended). Make all fieldsprivateandfinal(so direct access is denied and values are locked). Initialize all fields strictly via a constructor, and do not provide any setter methods.
Q: What is synchronization in multithreading?
A: Synchronization ensures that only one thread can access a critical section of code or a shared resource at a time, preventing race conditions and ensuring data consistency in a multi-threaded environment.
2. Spring Boot & APIs
Q: How do you secure the endpoints of a Spring Boot application?
A: Endpoints can be secured using Spring Security. Common methods include implementing JSON Web Tokens (JWT) for stateless API authentication, using OAuth2 for third-party authorization, or configuring basic form-based/HTTP Basic authentication for internal tools.
Q: How do you reduce the response time and optimize the performance of a Spring Boot application?
A:
- Implementing caching mechanisms (like Redis) for frequently accessed data.
- Optimizing database queries and using connection pooling (like HikariCP).
- Utilizing asynchronous processing (
@Async) for non-critical background tasks. - Implementing load balancing across multiple server instances.
- Disabling unnecessary Spring Boot auto-configurations to speed up startup and execution.
Q: What is the difference between @Controller and @RestController?
A:
@Controller: Used to handle traditional web pages. It returns view names that are resolved into HTML/JSP templates.@RestController: Used for building APIs. It automatically combines@Controllerand@ResponseBody, ensuring that returned objects are serialized directly into JSON/XML and sent back in the HTTP response body.
Q: Explain the difference between @RequestParam, @PathVariable, and @RequestBody.
A:
@RequestParam: Extracts data from the query parameters in the URL (e.g.,?id=123).@PathVariable: Extracts dynamic values directly from the URI path itself (e.g.,/users/123).@RequestBody: Reads the entire HTTP request payload (usually JSON) and deserializes it into a Java object.
Q: How do you handle concurrent users in a Spring Boot application?
A: By implementing embedded server thread pools (Tomcat/Undertow), utilizing database connection pooling, optimizing stateless session management (using JWT instead of server memory), and deploying the application behind a load balancer across multiple servers.
Q: Scenario: Your application currently handles 1,500 users. Suddenly, traffic spikes to 15,000 users. How would you handle this situation?
A: I would handle this by scaling the resources horizontally—adding more server instances. I would utilize load balancers to distribute the incoming traffic evenly across the new servers and implement Auto-scaling in the cloud environment (like AWS EC2 Auto Scaling or Kubernetes HPA) to adjust resources dynamically based on the live traffic load.
Q: What is the difference between Future and CompletableFuture?
A:
Future: A simple placeholder for an asynchronous result that isn't ready yet. It is severely limited because retrieving the result requires calling a blocking.get()method.CompletableFuture: An advanced, non-blocking class. It allows you to manually set results, easily chain multiple tasks together step-by-step, and effectively handle asynchronous exceptions without blocking the main thread.
3. Databases (SQL)
Q: Explain Indexes, Stored Procedures, ACID Properties, Triggers, and Normalization.
A:
- Indexes: Data structures that dramatically speed up data retrieval (reads) but can slow down write operations and consume extra disk space.
- Stored Procedures: Precompiled SQL code saved in the database for repeated use and executing complex business logic.
- ACID Properties: Ensure reliable database transactions. Stands for Atomicity, Consistency, Isolation, and Durability.
- Triggers: Special stored procedures that automatically execute (fire) in response to specific data changes (INSERT, UPDATE, DELETE) on a table.
- Normalization: The process of organizing database tables to reduce data redundancy and improve data integrity by dividing large tables into smaller, logically related ones.
Q: SQL Queries on an Employee table (ID, Name, Salary, City, Department)
1. Find the maximum salary of employees from the same city:
SELECT City, MAX(Salary)
FROM Employee
GROUP BY City;
2. Find the sum of all employees' salaries based on the department:
SELECT Department, SUM(Salary)
FROM Employee
GROUP BY Department;
3. Find the second highest salary of the employee:
SELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
4. Coding Questions
During the technical round, the candidate was asked to solve the following algorithmic challenges:
- Stream API Frequency: You are given a
List<String>. Write code using the Java Stream API to calculate the frequency of each string in the list. - Linked List Reordering: You are given a Linked List. Write an algorithm to reorder the linked list (typically $L_0 \rightarrow L_n \rightarrow L_1 \rightarrow L_{n-1} \dots$).
- Container With Most Water: A classic algorithmic problem (often found on LeetCode) to find two lines on an axis that, together with the x-axis, forms a container that holds the most water.