Coding Interview Question for Experienced Java Developer role series-7

These days if you want to succeed in the technical rounds, Coding questions hold a lot of weight. As they reveal a lot about the candidate's programming skills. This can be achieved by practicing only. Below series of questions will help you to understand better.

Ajay Rathod
4 min readMay 18, 2022

(A bit of context: I’ve conducted numerous interviews for software engineering positions in most of the MNCs. Additionally, there have been instances where I faced challenges during coding interviews due to lack of preparation.)

Are you preparing for a job interview as a Java developer?

Find my book Guide To Clear Java Developer Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Guide To Clear Spring-Boot Microservice Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Download the sample copy here: Guide To Clear Java Developer Interview[Free Sample Copy]

Guide To Clear Spring-Boot Microservice Interview[Free Sample Copy]

Coding challenge

Java 8 Stream API coding questions:

Find all employees who live in ‘Pune’ city, sort them by their name, and print the names of employees.

Java-8 stream API questions hold a lot of weight in the coding round we must be well-prepared.

employeeList.stream()
.filter(e -> e.getCity().equalsIgnoreCase("Pune"))
.sorted(Comparator.comparing(Employee::getName))
.forEach(e -> System.out.println(e.getName()));

Find the Book id from a map whose name is “java” using stream API java-8.

Map<String, String> books = new HashMap<>();
books.put("123-A", "DS");
books.put( "324-A", "c++");
books.put("213-B", "Java");
Ans -To create stream for map use entrySet and then stream.
Optional<String> result = books.entrySet().
stream().
filter(e -> e.getValue().equalsIgnoreCase("Java")).
map(Map.Entry::getKey).findFirst();
System.out.println(result);

Find the Output

package stream;

public class Main {
public static void main(String[] args) {
SuperC c=new SubCla();
c.methodToOverride1();
c.methodToOverride2();
}
}
class SuperC {
public void methodToOverride1(){
System.out.println("in methodToOverride1");
}
public void methodToOverride2(){
System.out.println("in methodToOverride2");
}
}
class SubCla extends SuperC{
public void methodToOverride1(){
System.out.println("in methodToOverride1-sub");
}
public void methodToOverride2(){
System.out.println("in methodToOverride2-sub");
}
}

Find the occurrence of names of employees from the List<Employee>, and find the frequency of each name.

public class Test {
public static void main(String[] args) {
Employee emp1 = new Employee(1,"Ajay",100);
Employee emp2 = new Employee(1,"name",100);
Employee emp3 = new Employee(1,"Ajay",100);
Employee emp4 = new Employee(1,"name",100);
Employee emp5 = new Employee(1,"Ajay",100);

List<Employee> empList = Arrays.asList(emp1,emp2,emp3,emp4,emp5);
Map<String,Long> answer = empList.stream().collect(Collectors.groupingBy(Employee::getName,Collectors.counting()));
System.out.println(answer);

}
}

Query to find employee numbers in each department.

SELECT emp_dept, COUNT(*) FROM emp_details GROUP BY emp_dept;

Write a program for the Quick sort algorithm.

class Solution {
public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
public void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}

int p = partition(nums, left, right);
quickSort(nums, left, p - 1);
quickSort(nums, p + 1, right);
}
/*
[left, i) be # less than nums[pivot]
[i, j] undiscovered
(j, right] be # greater or equal than nums[pivot]

*/
public int partition(int[] nums, int left, int right) {
int pivot = right;
int j = right - 1;
int i = left;
while (i <= j) {
if (nums[i] < nums[pivot]) {
i++;
} else if (nums[j] >= nums[pivot]) {
j--;
} else {//nums[i] >= nums[pivot] && nums[j] < nums[pivot]
swap(nums, i, j);
i++;
j--;
}
}//postcondition: i > j
swap(nums, i, pivot);
return i;
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

Find the output below code. Will this compile if not, what can be done to compile it?

public class MainTest {
public static void main(String[] args) {
Parent parent = new Child();
//Will this below line compile
parent.foo();
//Will this below line compile
parent.bar();
}
}

class Parent {

public void foo() {
System.out.println("Parent :: foo()");
}
}

class Child extends Parent {

@Override
public void foo() {
System.out.println("Child :: foo()");
}

public void bar() {
System.out.println("Child :: bar()");
}
}

What would be the size of the HashSet? What can we do to stop HashSet from adding duplicate students with the same name? please tell us about the workaround.

public class HashSetTEst {

public static void doSomethingWithHashSet() {
HashSet<Student> student = new HashSet();
student.add(new Student("Ajay"));
student.add(new Student("Ajay"));
System.out.println("sizze of hashset : "+student.size());
}

public static void main(String[] args) {
doSomethingWithHashSet();

}
}
class Student {

String name;
public Student(String name) {
this.name = name;
}
}

The above problem works and adds two objects to the set, we have to add logic to prevent that.

Program to convert below binary string array?

input:
000000000
011111110
010000010
010000010
010000010
010000010
011111110
000000000

x = 0, y = 0

Output:

111111111
111111111
110000011
110000011
110000011
110000011
111111111
111111111

Types of fault tolerance mechanisms in Spring microservices?

There are a few ways we can use it.

  • Timeouts
  • Retries
  • Circuit Breaker
  • Deadlines
  • Rate limiters

Hystrix and Resiliancy4j are one of the java libraries which help with the above options.

Java 8 changes in memory optimization?

PermGen was removed and replaced with MetaSpace.Class and metadata moved to MetaSpace.MetaSpace is not contiguous with Java Heap and allocated out of Native memory. This means it can use available memory from the system upto max memory until… read below

  • MetaSpace size max limit can be configured using JVM options.
  • G1 starts supporting the concurrent unloading of Classes.
  • Code Cache is introduced. It stores compiled code by the JIT compiler.
  • Compressed Class Space is also introduced.

Some of my articles can help you to clear your Java Developer Interview.

How I got more than 10 offer letters as a Java Developer in 3 months.

How I Learned Java in Depth by preparing for Java Technical Interviews

Thanks for reading

  • 👏 Please clap for the story and follow me 👉
  • 📰 Read more content on my Medium (21 stories on Java Developer interview)

Find my books here:

--

--

Ajay Rathod

Software Engineer @Cisco | Java Programmer | AWS Certified | Writer | Find My Books on Java Interview here - https://rathodajay10.gumroad.com/