Introduction:
If you’re preparing for an interview at Persistent.com, a global software product development company, it’s essential to be well-prepared. In this blog post, we’ll explore three questions that were asked during interviews at Persistent.com. These questions will help you gauge the level of difficulty and the types of topics you might encounter during the interview process.
Problem Statement: Given a Map<Integer, Employee>
where the key represents the employee ID and the value is an Employee
object containing fields like id
, name
, salary
, department
, and address
, write a Java program to find the employee with the highest salary from each department.
Solution: To solve this problem, we can leverage the power of Java Streams and the Collectors
utility class. Here’s the solution:
// Employee.java
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Employee {
private final int id;
private final String name;
private final double salary;
private final String department;
private final String address;
}
// GroupByDemo.java
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class GroupByDemo {
private static final int EMPLOYEE_ID_1 = 1;
private static final String EMPLOYEE_NAME_1 = "John Doe";
private static final double EMPLOYEE_SALARY_1 = 5000.0;
private static final String EMPLOYEE_DEPARTMENT_1 = "IT";
private static final String EMPLOYEE_ADDRESS_1 = "123 Main St.";
// ... (define constants for other employees)
private static void printHighestSalaryEmployee(String department, Optional<Employee> highestSalaryEmployee) {
if (highestSalaryEmployee.isPresent()) {
Employee employee = highestSalaryEmployee.get();
System.out.println("Department: " + department + ", Highest Salary Employee: " + employee);
} else {
System.out.println("Department: " + department + ", No employees found");
}
}
public static void main(String[] args) {
Map<Integer, Employee> employeeIdToEmployeeMap = new HashMap<>();
employeeIdToEmployeeMap.put(EMPLOYEE_ID_1, new Employee(EMPLOYEE_ID_1, EMPLOYEE_NAME_1, EMPLOYEE_SALARY_1, EMPLOYEE_DEPARTMENT_1, EMPLOYEE_ADDRESS_1));
// ... (add other employees)
Map<String, Optional<Employee>> highestSalaryByDepartment = employeeIdToEmployeeMap.values().stream()
.collect(Collectors.groupingBy(
Employee::getDepartment,
Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))
));
highestSalaryByDepartment.forEach((department, highestSalaryEmployee) ->
printHighestSalaryEmployee(department, highestSalaryEmployee)
);
}
}
Explanation:
- We start by creating a stream from the
employeeMap.values()
, which gives us aStream<Employee>
. - We then use the
collect
method withgroupingBy
andmaxBy
collectors to group the employees by their department and find the employee with the highest salary in each department group. - The
groupingBy(Employee::getDepartment, ...)
operation groups the employees by their department. The second argument togroupingBy
is a downstream collector that processes each group of employees. - The
maxBy(Comparator.comparingDouble(Employee::getSalary))
collector finds the employee with the maximum salary in each group, using thecomparingDouble
comparator on thegetSalary
method. - The result of the
collect
operation is aMap<String, Optional<Employee>>
where the keys are the department names, and the values areOptional<Employee>
objects representing the employee with the highest salary in that department (orOptional.empty()
if the department has no employees). - Finally, we iterate over the entries in the resulting map and print the department name and the highest salary employee for each department.
Question 2: Finding Pairs of Elements with a Given Sum in a Sorted Array
Problem Statement: Given a sorted array of integers in the range [-10,000, 10,000] and a target sum, find all pairs of elements in the array that sum up to the target value.
Examples:
- Input:
nums = [1, 12, 13], target = 25
Output: [] - Input:
nums = [1, 12, 13, 12], target = 25
Output: [[12, 13]] - Input:
nums = [-50, 25], target = 25
Output: [[-50, 25]] - Input:
nums = [-30, -27, -25, -21, -15, -2, -1, 0, 1, 2, 3, 24, 35, 55], target = 25
Output: [[-25, 50]]
Solution: To solve this problem, we can use the two-pointer technique, which takes advantage of the sorted nature of the input array. Here’s the solution:
javaCopy codeimport java.util.ArrayList;
import java.util.List;
public class PairSum {
public static List<List<Integer>> getAllPairs(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
int left = 0;
int right = nums.length - 1;
while (left < right) {
int currentSum = nums[left] + nums[right];
if (currentSum == target) {
result.add(List.of(nums[left], nums[right]));
left++;
right--;
} else if (currentSum < target) {
left++;
} else {
right--;
}
}
return result;
}
public static void main(String[] args) {
int[] nums1 = {1, 12, 13};
int[] nums2 = {1, 12, 13, 12};
int[] nums3 = {-50, 25};
int[] nums4 = {-30, -27, -25, -21, -15, -2, -1, 0, 1, 2, 3, 24, 35, 55};
System.out.println(getAllPairs(nums1, 25)); // Output: []
System.out.println(getAllPairs(nums2, 25)); // Output: [[12, 13]]
System.out.println(getAllPairs(nums3, 25)); // Output: [[-50, 25]]
System.out.println(getAllPairs(nums4, 25)); // Output: [[-25, 50]]
}
}
Explanation:
- The
getAllPairs
method takes an input arraynums
and a target sumtarget
. - We initialize two pointers,
left
andright
, to the start and end of the sorted array, respectively. - We iterate over the array using these pointers, calculating the sum of the elements at
left
andright
. - If the sum is equal to the target sum, we add the pair to the result list, and move both pointers inwards.
- If the sum is less than the target sum, we increment the
left
pointer to consider larger values. - If the sum is greater than the target sum, we decrement the
right
pointer to consider smaller values. - We continue this process until the pointers meet in the middle, ensuring that all possible pairs are considered.
- Finally, we return the result list containing all pairs that sum up to the target sum.
Conclusion: In this blog post, we covered two interview questions from [Company Name] related to Java Streams and array manipulations. The first question focused on finding the highest salary employee from each department using Java Streams and the Collectors
utility class. The second question involved finding pairs of elements in a sorted array that sum up to a given target value, using the two-pointer technique.
By mastering these concepts and practicing similar problems, you’ll be better prepared for technical interviews at [Company Name] and other companies in the software development industry.