Blog Interview Experiences

Interview Questions from Persistent.com

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:

Java
// 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:

  1. We start by creating a stream from the employeeMap.values(), which gives us a Stream<Employee>.
  2. We then use the collect method with groupingBy and maxBy collectors to group the employees by their department and find the employee with the highest salary in each department group.
  3. The groupingBy(Employee::getDepartment, ...) operation groups the employees by their department. The second argument to groupingBy is a downstream collector that processes each group of employees.
  4. The maxBy(Comparator.comparingDouble(Employee::getSalary)) collector finds the employee with the maximum salary in each group, using the comparingDouble comparator on the getSalary method.
  5. The result of the collect operation is a Map<String, Optional<Employee>> where the keys are the department names, and the values are Optional<Employee> objects representing the employee with the highest salary in that department (or Optional.empty() if the department has no employees).
  6. 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:

  1. The getAllPairs method takes an input array nums and a target sum target.
  2. We initialize two pointers, left and right, to the start and end of the sorted array, respectively.
  3. We iterate over the array using these pointers, calculating the sum of the elements at left and right.
  4. If the sum is equal to the target sum, we add the pair to the result list, and move both pointers inwards.
  5. If the sum is less than the target sum, we increment the left pointer to consider larger values.
  6. If the sum is greater than the target sum, we decrement the right pointer to consider smaller values.
  7. We continue this process until the pointers meet in the middle, ensuring that all possible pairs are considered.
  8. 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.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over