Bounteous x Accolite
10+ Cognizant Interview Questions and Answers
Q1. Sync Api vs Async Api, Sync Microservice and Async microservice example
Sync API waits for a response before continuing, while Async API allows the program to continue executing without waiting for a response.
Sync API is blocking and waits for a response before proceeding
Async API is non-blocking and allows the program to continue executing while waiting for a response
Sync microservice handles requests sequentially, while Async microservice can handle multiple requests concurrently
Example of Sync API: REST API that waits for a response before ret...read more
Q2. Write a REST api to fetch user details using userId.
Create a REST api to fetch user details using userId
Create a GET endpoint /users/{userId} to fetch user details
Use userId as a parameter in the endpoint
Return user details in JSON format
Handle errors for invalid userId
Q3. Write a global exception handler class to handle UserNotFound exception.
Create a global exception handler class for UserNotFound exception.
Create a class that extends ExceptionHandler class
Override the handleException method to handle UserNotFound exception
Implement the logic to handle the exception, such as logging or returning a custom error message
Q4. What are different sorting algorithms?
Q5. SOLID Principle and best coding practices
SOLID principles and best coding practices are essential for creating maintainable and scalable software.
S - Single Responsibility Principle: Each class should have only one responsibility.
O - Open/Closed Principle: Classes should be open for extension but closed for modification.
L - Liskov Substitution Principle: Subtypes should be substitutable for their base types.
I - Interface Segregation Principle: Clients should not be forced to depend on interfaces they do not use.
D - ...read more
Q6. Optional Class, Stream.map() vs Stream.flatMap()
Stream.map() transforms each element in a stream, while Stream.flatMap() transforms each element into a stream of values.
map() applies a function to each element in a stream and returns a new stream with the transformed elements.
flatMap() applies a function that returns a stream for each element in the original stream, then flattens the streams into a single stream of values.
Example: map() - Stream.of(1, 2, 3).map(x -> x * 2) returns Stream.of(2, 4, 6).
Example: flatMap() - St...read more
Q7. Print all duplicate elements in an Array
Print duplicate elements in an Array of strings
Iterate through the array and use a HashMap to store frequency of each element
Print elements with frequency greater than 1 as duplicates
Q8. Advantages of IOC in spring and DI
IOC in Spring and DI offer flexibility, maintainability, and testability in software development.
Promotes loose coupling between components
Allows for easier unit testing and mocking
Facilitates easier configuration and management of dependencies
Enables better separation of concerns
Promotes reusability of components
Q9. Top view of a binary tree
A top view of a binary tree shows the nodes visible from the top when looking down from the root node.
The top view of a binary tree can be obtained by performing a level order traversal and keeping track of the horizontal distance of each node from the root.
Nodes with the same horizontal distance are at the same level in the top view.
Example: For the binary tree 1 -> 2 -> 3 -> 4 -> 5, the top view would be 1 -> 2 -> 3 -> 4 -> 5.
Q10. Spring IOC and types.
Spring IOC (Inversion of Control) is a design pattern where the control of object creation and lifecycle is shifted to a container.
In Spring IOC, objects are created and managed by the Spring container.
Types of Spring IOC include Constructor-based dependency injection and Setter-based dependency injection.
Example: In Constructor-based dependency injection, dependencies are provided through the constructor of a class.
Example: In Setter-based dependency injection, dependencies ...read more
Q11. Circuit Breaker and its states
Circuit Breaker is a design pattern used in software development to prevent system overload and failures.
Circuit Breaker monitors the number of failures and opens when a threshold is reached.
It can be in states like closed, open, or half-open.
Closed state allows normal operation, open state prevents further requests, and half-open state allows limited requests to check if the system is back to normal.
Examples include Hystrix in Java and Polly in .NET.
Top HR Questions asked in Cognizant
Interview Process at Cognizant
Top Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month