Interview Experience at Yash Technologies: Technical Round for Java and SpringBoot

Author Profile Pic
Anurag
Published on Fri Jun 28 2024 ~ 6 min read
Interview Experience at Yash Technologies: Technical Round for Java and SpringBoot

My technical interview with Yash Technologies was a thorough and engaging experience, especially focusing on my expertise in Java and SpringBoot. With 3 years of experience under my belt, I felt confident but aware of the challenge ahead. The interview comprised several questions, ranging from theoretical concepts to practical coding problems. Here's a detailed account of the questions asked and how I approached them.


Q1. What is Parallel Stream and what is the difference between Parallel and Sequential Stream?

Answer:

Parallel Stream in Java 8 allows operations to be executed concurrently, leveraging multi-core processors. This can significantly enhance performance for large datasets. The key difference between Parallel Stream and Sequential Stream is in their execution:

  • Sequential Stream: Processes elements one at a time, in a single thread.
  • Parallel Stream: Splits the stream into multiple parts, processing elements concurrently using multiple threads.


Q2. Write Java 8 code using Stream API to filter a List of employees by department.

Answer:

List<Employee> employees = // assume this is populated

List<Employee> filteredEmployees = employees.stream()
    .filter(employee -> "IT".equals(employee.getDepartment()))
    .collect(Collectors.toList());


Q3. Extract the distinct departments and print them comma-separated.

Answer:

String departments = employees.stream()
    .map(Employee::getDepartment)
    .distinct()
    .collect(Collectors.joining(","));
System.out.println(departments);


Q4: Sort the list by department and by employees within the same department.


List<Employee> employees = // Assume this is initialized

List<Employee> sortedEmployees = employees.stream()
                                          .sorted(Comparator.comparing(Employee::getDepartment)
                                          .thenComparing(Employee::getName))
                                          .collect(Collectors.toList());


Q5: How many ways can we create an Optional object?

Answer:

  1. Optional.empty(): Creates an empty Optional.
  2. Optional.of(value): Creates an Optional with the given non-null value.
  3. Optional.ofNullable(value): Creates an Optional with the given value, which can be null.


Q6: Difference between Optional.of and Optional.ofNullable("value").

Answer:

  • Optional.of(value): Throws NullPointerException if the value is null.
  • Optional.ofNullable(value): Returns an empty Optional if the value is null, otherwise returns an Optional containing the value.


Q7: Write a POST endpoint in Spring Boot.


@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

    @PostMappingpublic ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
        // Assuming a service exists to save the employee
        Employee savedEmployee = employeeService.save(employee);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedEmployee);
    }
}


Q8: Write a custom exception.


public class EmployeeNotFoundException extends RuntimeException {
    public EmployeeNotFoundException(String message) {
        super(message);
    }
}


Q9: Write a global exception handler.


@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(EmployeeNotFoundException.class)
    public ResponseEntity<String> handleEmployeeNotFoundException(EmployeeNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleGeneralException(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred");
    }
}


Q10: Write JUnit code for the service layer to get employees.


@RunWith(SpringRunner.class)
@SpringBootTest
public class EmployeeServiceTest {

    @Autowired
    private EmployeeService employeeService;
    
    @MockBean
    private EmployeeRepository employeeRepository;
    
    @Test
    public void testGetAllEmployees() {
            List<Employee> employees = Arrays.asList(
                    new Employee("John", "IT"),
                    new Employee("Jane", "HR")
            );
    
            Mockito.when(employeeRepository.findAll()).thenReturn(employees);
    
            List<Employee> result = employeeService.getAllEmployees();
            Assert.assertEquals(2, result.size());
        }
    }


Q11: INNER JOIN, OUTER JOIN, LEFT JOIN, RIGHT JOIN?

Answer:

  • INNER JOIN: Returns records that have matching values in both tables.
  • LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and the matched records from the right table. Unmatched records from the right table will be null.
  • RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and the matched records from the left table. Unmatched records from the left table will be null.
  • FULL OUTER JOIN: Returns all records when there is a match in either the left or right table. Records without a match will be null.


Q12: JWT in Spring Boot?

Answer:

  • JWT (JSON Web Token): A compact, URL-safe means of representing claims to be transferred between two parties. JWTs are often used for authentication and authorization purposes.


Implementation in Spring Boot:

  1. Dependency: Add the necessary dependencies in pom.xml.
  2. Security Config: Configure Spring Security to use JWT.
  3. Filter: Create a filter to validate the JWT.
  4. Service: Create a service to generate and validate JWTs.
  5. Controller: Use the JWT in your authentication endpoints.


Conclusion

These questions and answers cover some of the essential questions I encountered in an interview with Yash Technology. Good luck with your preparation, and remember to understand the concepts thoroughly to demonstrate your expertise effectively!

Comments


Loading...

Post a Comment

Address

Nirvana Apt, Hinjeadi, Pune, Maharastra - 411057 (India)

Website
Site : www.anucodes.in
Social