Interview Experience with IBM for an Experienced Java Developer Role

Author Profile Pic
Anurag
Published on Sun Jun 23 2024 ~ 10 min read
Interview Experience with IBM for an Experienced Java Developer Role

As an experienced Java developer with three years under my belt, I recently had the opportunity to interview with IBM. This blog post details my experience, the questions asked, and the solutions I provided. This might help you prepare for similar roles and understand what to expect.


Interview Date: 15th June 2024

Role: Experienced Java Developer

Duration: 1 hour


Verbal Questions


Q1: Write a code to find the first non-repeating character in a string.


Here’s the solution I provided:


import java.util.HashMap;
import java.util.Map;

class Solution {
    public static void main(String[] args) {
        String word = "simplest"; // i
        Map<Character, Integer> charCount = new HashMap<>();

        for (char c : word.toCharArray()) {
            charCount.put(c, charCount.getOrDefault(c, 0) + 1);
        }

        for (char c : word.toCharArray()) {
            if (charCount.get(c) == 1) {
                System.out.println(c);
                break;
            }
        }
    }
}


Q2: How to implement Serialize and Deserialize in Java? Fix the bug in the following code:


import java.io.Serializable;
import java.util.Map;
import java.util.HashMap;

class House implements Serializable {
    private static final long serialVersionUID = 1L; // Added serialVersionUID

    public House(int number) {
        super();
        this.number = number;
        this.cities = new HashMap<>(); // Initialize cities map
    }

    Wall wall;
    int number;
    Map<String, String> cities;
}

class Wall implements Serializable {
    private static final long serialVersionUID = 1L; // Added serialVersionUID

    int length;
    int breadth;
    int color;
}


Q3: Create a custom annotation in Java to have a minimum length of 5.


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MinLength {
    int value() default 5;
}


Q4: Create a custom exception in Java.


public class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}


Q5: How to autowire an interface object in Java if we have multiple classes implementing the interface?


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

interface Car {
    void speed();
}

@Component("swift")
class Swift implements Car {
    // Implement speed
}

@Component("kia")
class KIA implements Car {
    // Implement speed
}

@Service
class CarService {
    @Autowired@Qualifier("swift")
    Car car;
}


Q6: Reverse the ArrayList of elements in Java using Stream.


import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<String> listOfStrings = Arrays.asList("Facebook", "Twitter", "YouTube", "WhatsApp", "LinkedIn");

        List<String> reversedList = listOfStrings.stream()
                                                 .sorted(Collections.reverseOrder())
                                                 .collect(Collectors.toList());

        System.out.println("Reversed ArrayList: " + reversedList);
    }
}


Q7: What is the parent interface of Collections?

Answer: The parent interface of Collections is Iterable.


Q8: Types of Functional Interface.

Answer:

  • Predicate
  • Consumer
  • Supplier
  • Function


Q9: What does the mvn clean command do?

Answer: It deletes/cleans the target directory.


Q10: Maven command to run the Spring Boot project?

  • Answer: mvn spring-boot:run


Q11: How to read the value from application.properties file in Java?

Answer:

  • Using @Value annotation to access the values defined in the application.properties.
  • Using @Autowired and Environment.


Q12: What is the latest Spring Boot version?

  • Answer: As of my last update, the latest version was Spring Boot 2.5.5.


Q13: How to load some properties while the server starts in Java?

  • Answer: You can use Spring Boot's @ConfigurationProperties annotation to load properties during server startup.


Q14: How to pass command line arguments in Spring Boot?

  • Answer: Use mvn spring-boot:run -Dspring-boot.run.arguments=--person.name=Test


Q15: What are the features of dependency injection in Spring Boot?

Answer: Loose coupling, easier unit testing, and improved code maintainability.


Q16: Types of Dependency Injection?

Answer: Constructor Injection, Setter Injection, and Interface Injection.


Q17: Difference between HashMap, HashSet, and TreeSet.

Answer:

  • HashMap:
  • Implements the Map interface.
  • Stores key-value pairs.
  • Uses a hash table data structure.
  • Allows one null key and multiple null values.
  • HashSet:
  • Implements the Set interface.
  • Stores unique elements.
  • Uses a hash table internally.
  • Allows one null element.
  • TreeSet:
  • Implements the Set interface.
  • Stores unique elements in sorted order.
  • Uses a Red-Black tree internally.
  • Does not allow null elements.


Q18: How to create a thread in Java?

Answer: You can implement threads by extending the Thread class or by implementing the Runnable interface.


public class MyThread extends Thread {
    public void run() {
        System.out.println("Thread running");
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
    }
}

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("Thread running");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new MyRunnable());
        thread.start();
    }
}


Q19: Explain the life cycle of a thread.

Answer:

  • New: When a thread is created but has not yet started executing.
  • Runnable: After calling the start() method, but the thread may not be executing.
  • Running: When the CPU scheduler selects the thread for execution.
  • Blocked/Waiting: When a thread is waiting for a resource or another thread.
  • Timed Waiting: When a thread waits for a specified time.
  • Terminated: When a thread completes its execution or is explicitly terminated.


Q21: Optional Keyword in Java.

Answer: Optional is a container object that may or may not contain a non-null value, aiming to reduce NullPointerExceptions.


Q22: Java 8 code to convert a list of lists into a linear list.


public static void main(String[] args) {
    List<List<Integer>> listOfLists = new ArrayList<>();
    listOfLists.add(Arrays.asList(1, 2, 3));
    listOfLists.add(Arrays.asList(4, 5, 6));
    listOfLists.add(Arrays.asList(7, 8, 9));

    List<Integer> linearList = listOfLists.stream()
                                          .flatMap(List::stream)
                                          .collect(Collectors.toList());

    System.out.println("Linear list: " + linearList);
}


Q23: What is @SpringBootApplication Annotation?

Answer: This annotation marks the main class of a Spring Boot application, encapsulating @Configuration, @EnableAutoConfiguration, and @ComponentScan.


Q24: Different Spring Boot starter dependencies.

Answer:

  • spring-boot-starter
  • spring-boot-starter-web
  • spring-boot-starter-data-jpa
  • spring-boot-starter-data-mongodb
  • spring-boot-starter-security
  • spring-boot-starter-test
  • spring-boot-starter-actuator
  • spring-boot-starter-validation
  • spring-boot-starter-mail
  • spring-boot-starter-cache


Q25: Basic design patterns and examples.

Answer:

  • Singleton Pattern:



public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
  • Factory Method Pattern:


public interface Animal {
    void makeSound();
}

public class Dog implements Animal {
    public void makeSound() {
        System.out.println("Woof");
    }
}

public class Cat implements Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

public interface AnimalFactory {
    Animal createAnimal();
}

public class DogFactory implements AnimalFactory {
    public Animal createAnimal() {
        return new Dog();
    }
}

public class CatFactory implements AnimalFactory {
    public Animal createAnimal() {
        return new Cat();
    }
}


Conclusion

Interviewing with IBM was a rigorous but enriching experience. The technical questions tested both my theoretical knowledge and practical skills in Java. I hope this detailed account helps fellow developers prepare for their interviews. Good luck!


Feel free to ask any questions or share your interview experiences in the comments below!

Comments


Loading...

Post a Comment

Address

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

Website
Site : www.anucodes.in
Social