Optum Interview Experience—Round 1

Optum Interview Experience—Round 1

In this article, I will provide a detailed explanation of the questions I faced during Round 1 of the OPTUM interview. The focus was mainly on core Java concepts, the Spring Framework, Multithreading, and Kubernetes. Below are the questions, along with in-depth explanations.


1. Java 8 Features?

Java 8 introduced several significant features to improve the productivity of developers and support modern programming practices. Some of the key features include:

  • Lambda Expressions: A short block of code that takes in parameters and returns a value. It helps in passing code as parameters (for example, in functional interfaces).

    • Example:

        (a, b) -> a + b;  // Lambda that sums two numbers
      
  • Streams API: A new abstraction to process sequences of elements (like collections) in a functional style.

    • Example:

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
      
  • Optional: A container object which may or may not contain a non-null value. Used to avoid NullPointerException.

    • Example:

        Optional<String> name = Optional.ofNullable(getName());
        name.ifPresent(n -> System.out.println(n));
      
  • Default Methods in Interfaces: Interfaces can now have method implementations with the default keyword. This was introduced to add functionality to interfaces without breaking existing code.

    • Example:

        interface MyInterface {
            default void greet() {
                System.out.println("Hello, world!");
            }
        }
      

2. Abstract Class vs Interface with Default Method. Any Advantage?

  • Abstract Class:

    • Can have both abstract and non-abstract methods.

    • It can maintain state through fields (variables).

    • It can have constructors, which interfaces cannot have.

  • Interface with Default Methods:

    • Interfaces traditionally couldn’t have method implementations. However, default methods allow interfaces to have method implementations, making them more flexible without affecting backward compatibility.

    • Advantage: If you need to add a new method to an interface, the default implementation does not break existing classes that implement that interface.

Real-world Example:
Imagine an old version of a system using an interface with methods that have no implementation. Later, as the system evolves, you need to add a method. Instead of changing all classes that implement the interface, you can provide a default implementation.


3. Wrapper Classes?

Wrapper classes in Java are used to wrap primitive data types (like int, char, boolean, etc.) into objects. Java provides these classes in the java.lang package for each primitive type, such as Integer, Double, Character, etc.

  • Why use them?

    • They are needed when you want to treat primitive types as objects (for example, to store them in collections like ArrayList, which only stores objects).

    • They provide utility methods, such as parsing or converting strings into primitives.

Example:

int a = 10;
Integer obj = Integer.valueOf(a);  // Wrapping primitive into an Integer object

4. Method clone() is part of what?

The clone() method is part of the Object class, which is the root class of all Java classes. This method is used to create a shallow copy of an object. A shallow copy means the fields of the object are copied, but nested objects are not.

Example:

class Person implements Cloneable {
    String name;

    Person(String name) {
        this.name = name;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

Person p1 = new Person("John");
Person p2 = (Person) p1.clone();  // p2 is a shallow copy of p1

5. Difference Between final, finally, and finalize?

  • final: Used to declare constants, prevent method overriding, and prevent inheritance.

    • Example:

        final int MAX_VALUE = 100;  // Constant
      
  • finally: A block used in exception handling that always executes, regardless of whether an exception is thrown or not.

    • Example:

        try {
            // code that may throw exception
        } finally {
            // code that will always execute
        }
      
  • finalize: A method in the Object class that is called by the garbage collector before an object is removed from memory. It is generally used for cleanup operations.

    • Example:

        protected void finalize() {
            // cleanup code
        }
      

6. What is the volatile keyword?

The volatile keyword in Java ensures that a variable's value is always read from the main memory, rather than a thread’s local cache. This makes it useful in multi-threaded environments where multiple threads might be modifying the value of the same variable.

  • Why use it?: To ensure visibility of changes to variables across all threads.

Example:

private volatile boolean flag = false;

7. Can we make an array volatile?

You can declare the reference to an array as volatile, but not the array elements. This means the reference to the array is treated with special visibility, but the array's contents will not be automatically synchronized across threads.

Example:

private volatile int[] numbers = new int[10];

However, the individual elements of the array are not volatile by default.


8. Use of yield() Method in Threading?

Thread.yield() is a static method in the Thread class that allows the currently executing thread to voluntarily give up the CPU to another thread of the same or higher priority. It’s a way for a thread to hint to the scheduler that it’s willing to yield its execution time.

Example:

public void run() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            Thread.yield();  // Yield CPU time to another thread
        }
        System.out.println(i);
    }
}

9. How do Intrinsic and Non-Intrinsic Locks Work?

  • Intrinsic Locks (Monitor Locks): These are built into Java objects. Every object in Java has an intrinsic lock that can be obtained via the synchronized keyword.

  • Non-Intrinsic Locks: These are locks provided by classes like ReentrantLock in the java.util.concurrent.locks package. They offer more control over the locking mechanism (e.g., try-lock, timed lock, etc.).


10. Difference Between sleep() and wait()?

  • sleep(): Makes the current thread pause for a specified time without releasing the lock it holds. It’s a static method in Thread.

  • wait(): Makes the current thread pause and releases the lock it holds, allowing other threads to acquire it. It’s a method in Object.

Example:

// sleep() example
Thread.sleep(1000);  // Current thread pauses for 1 second

// wait() example
synchronized (lock) {
    lock.wait();  // Current thread waits and releases the lock
}

11. Difference Between Hashtable and HashMap?

  • Hashtable:

    • Synchronized, so it is thread-safe.

    • Can have only one thread accessing the data at a time.

    • Does not allow null keys or null values.

  • HashMap:

    • Not synchronized, so not thread-safe by default.

    • Allows null keys and values.

Real-world Example:
If you're building a multi-threaded application that requires thread-safety when accessing a map, use Hashtable or consider using ConcurrentHashMap instead. For single-threaded applications or when managing threads manually, HashMap is a better choice due to its superior performance.


12. Difference Between HashMap and ConcurrentHashMap?

  • HashMap:

    • Not thread-safe by default.

    • It can have issues when multiple threads try to modify the map concurrently.

  • ConcurrentHashMap:

    • Thread-safe, specifically designed for concurrent access.

    • Supports concurrent reads and writes.

    • Locks only a portion of the map (not the entire map), allowing for better performance under high concurrency.

Real-world Example:
Imagine a scenario where multiple threads are trying to update a shared map (for example, updating counters for users). A ConcurrentHashMap ensures thread safety, whereas a regular HashMap could lead to unpredictable results due to data corruption.


13. When to Use HashSet and TreeSet?

  • HashSet:

    • Implements the Set interface.

    • Does not maintain any order of elements (unordered collection).

    • Provides constant-time complexity for basic operations (add, remove, contains).

  • TreeSet:

    • Implements the SortedSet interface.

    • Maintains elements in a sorted (natural or custom comparator-based) order.

    • Slightly slower than HashSet because of the need to maintain order.

Real-world Example:
Use a HashSet when you need fast lookups and don’t care about order. Use a TreeSet when you need your elements to be sorted, for example, maintaining a sorted list of students based on their scores.


14. hashCode and equals? When to Override Them?

  • hashCode():

    • Returns a hash value for the object, used primarily in hash-based collections like HashMap, HashSet.

    • If two objects are considered equal according to equals(), they must return the same hashCode.

  • equals():

    • Compares two objects for equality.

    • The default implementation compares references, but it should be overridden to compare the actual content of objects.

When to Override:
Override hashCode and equals in objects that you plan to use as keys in a HashMap or store in a HashSet to ensure the correct behavior of these collections.

Example:

@Override
public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    Person person = (Person) obj;
    return name.equals(person.name);  // Compare fields for equality
}

@Override
public int hashCode() {
    return Objects.hash(name);  // Use the same field as equals
}

15. Difference Between Comparator and Comparable?

  • Comparable:

    • Used to define the natural ordering of objects (e.g., sorting a list of Person objects by name).

    • Implements the compareTo() method.

  • Comparator:

    • Used when you want to define multiple ways to order objects, not just the natural order.

    • Implements the compare() method.

Real-world Example:

  • Comparable: A Person class that can be sorted by age (using compareTo()).

  • Comparator: You may create a separate comparator to sort Person objects by name or address.

class Person implements Comparable<Person> {
    private String name;
    private int age;

    @Override
    public int compareTo(Person other) {
        return Integer.compare(this.age, other.age);  // Natural order by age
    }
}

class NameComparator implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.name.compareTo(p2.name);  // Order by name
    }
}

16. Spring IOC Container?

The Spring Inversion of Control (IOC) Container is the core of the Spring Framework, responsible for managing beans in the application. It handles object creation, configuration, and dependencies. The container uses Dependency Injection (DI) to provide objects that are dependent on other objects.

There are two main types of containers in Spring:

  • BeanFactory (Basic container)

  • ApplicationContext (Advanced container with additional features such as event propagation, internationalization, etc.)

Example:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
MyBean bean = context.getBean(MyBean.class);

17. Spring Beans?

In Spring, a Bean is an object that is managed by the Spring IoC container. A bean is created, configured, and managed by the container, and it can be injected into other beans using Dependency Injection (DI).

Types of Beans:

  • Singleton Bean: A single instance is shared across the entire Spring container (default scope).

  • Prototype Bean: A new instance is created every time the bean is requested.

Example:

<bean id="myBean" class="com.example.MyBean" />

18. Difference Between BeanFactory and ApplicationContext?

  • BeanFactory:

    • The most basic container in Spring.

    • Lazy initialization of beans (beans are created only when requested).

  • ApplicationContext:

    • Extends BeanFactory and provides more features like event propagation, internationalization support, and automatic bean creation.

    • Eager initialization of beans (beans are created on startup unless specified as lazy).

Example:

// ApplicationContext is typically used in production code
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

19. Annotation Used in ApplicationContext to Initialize a Bean?

In Spring, beans are typically initialized using XML configuration, but annotations such as @Component, @Service, @Repository, and @Controller can be used to declare beans.

  • @Component: Marks a class as a Spring bean.

  • @Autowired: Automatically injects dependencies into a bean.

@Component
public class MyService {
    @Autowired
    private MyDao myDao;
}

20. Actuators in Spring Boot?

Spring Boot Actuators provide production-ready features such as health checks, metrics, application environment information, etc. They are typically used for monitoring and managing Spring Boot applications.

  • Common endpoints include:

    • /actuator/health: Displays health status of the application.

    • /actuator/metrics: Provides metrics related to the application’s performance.

Example:

management.endpoints.web.exposure.include=health,info

21. What Are the Possible Sources of Configuring External Beans?

External beans in Spring can be configured from several sources:

  • XML configuration: Declaring beans inside an XML file.

  • Annotation-based configuration: Using annotations like @Component to declare beans.

  • Java-based configuration: Using @Configuration and @Bean annotations to define beans in Java code.

  • Property files: Externalized configuration via properties files (application.properties).


22. Relationship Between Two Entities?

In object-oriented design, a relationship between two entities refers to how different classes or objects are related to each other in the domain model. The common types are:

  • One-to-One: One object of class A is associated with one object of class B.

  • One-to-Many: One object of class A is associated with multiple objects of class B.

  • Many-to-Many: Multiple objects of class A are associated with multiple objects of class B.

Example:
A Customer has multiple Orders (One-to-Many relationship).


23. Prototype Design Pattern?

The Prototype Design Pattern is used to create new objects by cloning existing objects. This is useful when object creation is complex or expensive, and cloning an existing object is more efficient.

Example:

class Prototype implements Cloneable {
    public Prototype clone() {
        try {
            return (Prototype) super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}

24. Scope in Spring Boot?

Spring provides several bean scopes to control the lifecycle and visibility of beans:

  • Singleton: One instance for the entire application (default).

  • Prototype: A new instance each time the bean is requested.

  • Request: One instance per HTTP request (used in web applications).

  • Session: One instance per HTTP session.


25. Advantages of Kubernetes (K8s)?

  • Scalability: Easily scale applications and services based on demand.

  • High Availability: Ensure application uptime with automatic failover.

  • Automation: Automate deployment, scaling, and management of containerized applications.

  • Self-Healing: Automatically replace failed containers.


26. Difference Between Containers and Pods?

  • Container: A lightweight, stand-alone, executable package of software that includes everything needed to run an application.

  • Pod: The smallest deployable unit in Kubernetes. A pod can have one or more containers that share the same network, storage, and configuration.


These detailed answers should give you a deeper understanding of the questions asked during OPTUM Round 1 and how to approach them in an interview setting.

Conclusion

Thank you for reading through my detailed interview experience at OPTUM! I hope the questions and answers provided here help you in preparing for your own interview process. Stay confident, and remember that each interview is an opportunity to learn and grow. If you have any questions or would like to share your own experience, feel free to leave a comment below!

Good luck with your preparations, and don't forget to check back for updates as I share more insights from Round 2 of my OPTUM interview experience.


Follow Me:

Did you find this article valuable?

Support Ritik Gupta Blog's by becoming a sponsor. Any amount is appreciated!