Spring Boot & Java
Interview Questions & Answers
Coding Challenges | Spring | Microservices | Java Core | Database
1 Time & Space Complexity Analysis
1️⃣
Q: What is the time complexity of your solution?
Time Complexity: O(n)
Space Complexity: O(1)
We traverse the array once and use only a few variables.
Q: Why is the single-pass approach with leftSum optimal?
● Recalculating left and right sums for each index would take O(n²) time.
● Using totalSum and leftSum allows computing rightSum instantly in O(1) during a single
traversal.
Q: What edge cases did you consider?
Examples:
◦ [] — empty array
◦ [100] — single element
◦ [0, 0, 0] — all zeros
◦ Large arrays
Expected: If the array has 0 or 1 element, a balance point cannot exist.
Q: What if multiple balance points exist?
Answer: Return the first index found.
2️⃣ Spring Transaction
Q: What happens if one @Transactional method calls another @Transactional method?
Depends on the propagation type configured. By default (REQUIRED), the inner method joins the
existing transaction.
Q: How does transaction rollback work?
● By default, rollback occurs on unchecked exceptions (RuntimeException and Error).
● Checked exceptions do NOT trigger rollback unless specified with rollbackFor.
Q: What are transaction propagation types?
◦ REQUIRED — Join existing or create new (default)
◦ REQUIRES_NEW — Always create a new transaction
◦ SUPPORTS — Join if exists, else run non-transactionally
◦ NOT_SUPPORTED — Suspend current transaction
◦ MANDATORY — Must have an existing transaction
◦ NEVER — Throw if transaction exists
◦ NESTED — Nested savepoint within existing transaction
3️⃣ Spring MVC Testing
Q: How do you write test cases for REST APIs?
● Use @SpringBootTest with MockMvc or TestRestTemplate.
● Use @WebMvcTest to test controllers in isolation.
Q: How does Spring MVC testing work?
Spring MVC Test framework simulates HTTP requests without starting a full server, verifying controller
behavior through MockMvc.
Q: What is MockMvc?
● MockMvc is a Spring Test utility that simulates HTTP requests and verifies responses.
● Supports GET, POST, PUT, DELETE with status and body assertions.
[Link](get("/api/users")).andExpect(status().isOk());
4️⃣ Exception Handling
Q: How do you handle exceptions globally in Spring Boot?
● Use @RestControllerAdvice or @ControllerAdvice with @ExceptionHandler methods.
Q: What is @RestControllerAdvice / @ControllerAdvice?
● @ControllerAdvice — global exception handler for all controllers.
● @RestControllerAdvice — same but adds @ResponseBody automatically (returns JSON).
Q: How do you create custom exceptions?
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String msg) { super(msg); }
}
5️⃣ Bean Injection
Q: What is the purpose of @Qualifier?
Answer: @Qualifier resolves ambiguity when multiple beans of the same type exist. It specifies
which bean to inject by name.
Q: What happens when multiple beans of the same type exist?
● Spring throws NoUniqueBeanDefinitionException unless @Qualifier or @Primary is used.
● @Primary marks the default bean to inject when no @Qualifier is specified.
6️⃣ Spring Boot Features
Q: How do you enable scheduling in Spring Boot?
● Add @EnableScheduling to a @Configuration class or the main class.
● Annotate the method with @Scheduled.
Q: What is the purpose of @EnableScheduling?
Answer: It activates Spring's task scheduling infrastructure so @Scheduled methods are detected
and executed.
Q: What are @Enable annotations?
◦ @EnableScheduling — task scheduling
◦ @EnableAsync — asynchronous execution
◦ @EnableCaching — caching support
◦ @EnableWebSecurity — Spring Security
◦ @EnableTransactionManagement — transaction handling
7️⃣ Microservices Architecture
Q: What is API Gateway?
Answer: A single entry point for all clients that handles routing, authentication, rate limiting, and
load balancing across microservices.
Q: What is Circuit Breaker Pattern?
● Prevents cascading failures by stopping requests to a failing service.
● States: Closed (normal), Open (blocked), Half-Open (test recovery).
● Tools: Resilience4j, Hystrix.
Q: What tools are used in your project architecture?
◦ API Gateway (Spring Cloud Gateway / Kong)
◦ Service Discovery (Eureka / Consul)
◦ Circuit Breaker (Resilience4j)
◦ Messaging (Kafka / RabbitMQ)
◦ Config Server (Spring Cloud Config)
8️⃣ SQL / Database
Q: Have you implemented custom SQL filters?
● Yes — using JPA Specifications or custom @Query with native/JPQL.
● Criteria API can also build dynamic queries programmatically.
Q: What is the difference between JPQL and Native Query?
JPQL: Works with JPA entity names and fields (database-agnostic)
Native Query: Uses actual SQL syntax and table/column names (database-specific)
Q: Have you worked with Flyway database migrations?
● Flyway manages version-controlled database schema migrations.
● SQL scripts named V1__init.sql, V2__add_column.sql run in order automatically.
9️⃣ Java Core Questions
Q: What is a Functional Interface?
Answer: An interface with exactly one abstract method. It can be used as the target type for a
lambda expression or method reference.
@FunctionalInterface
public interface MyFunc { void execute(); }
Q: What is the purpose of Functional Interfaces?
● Enable lambda expressions and functional programming in Java.
● Built-in examples: Runnable, Callable, Comparator, Predicate, Function, Consumer, Supplier.
🔟 Java Streams Coding Question
Q: How do you find the second highest salary from a list of employees using Java Streams?
[Link]()
.map(Employee::getSalary)
.distinct()
.sorted([Link]())
.skip(1)
.findFirst()
.orElseThrow(() -> new RuntimeException("Not found"));
1️⃣1️⃣ JPA Testing
Q: What is the purpose of @DataJpaTest?
● Loads only JPA components (repositories, entities) — not the full application context.
● Uses an in-memory H2 database by default for fast, isolated tests.
● Rolls back each test transaction automatically.
1️⃣2️⃣ Scheduling
Q: How do you implement cron jobs in Spring Boot?
● Enable with @EnableScheduling on a config/main class.
● Use @Scheduled with cron expression on the method.
@Scheduled(cron = "0 0 9 * * MON-FRI")
public void runDailyReport() { ... }
Cron format: second minute hour day month weekday