Find Employees with Highest Salary

Java coding interview problem for Collections: Find Employees with Highest Salary.

Finding the employee with the highest salary is one of the most common Java Collections interview problems.

This problem teaches an important algorithm pattern:

Collection of Objects

        ↓

Compare Object Property

        ↓

Find Maximum Value

        ↓

Return Object

What is Finding Highest Salary Employee?

Given a list of employees, find the employee whose salary value is maximum.

Example:

Employees:

John     90000

Alice   120000

Bob      75000

Highest salary:

Alice

120000

Understanding Maximum Value Problems

Finding the highest salary is a special case of:

Find Maximum Element

For numbers:

[10,50,20]

Maximum:

50

For objects:

Employee objects

we need to decide:

Which property should be compared?

Employee:

id

name

salary

department

Comparison property:

salary

Employee Object and Salary Comparison

Java cannot automatically compare objects.

Example:

employee1 > employee2

is invalid.


We need comparison logic:

Employee A salary

vs

Employee B salary

Example:

John

salary = 90000


Alice

salary = 120000

Comparison:

90000 < 120000

Therefore:

Alice has higher salary

Why This Problem is Asked in Interviews?

This problem tests:

1. Object Traversal

Can you process:

List<Employee>

efficiently?


2. Comparison Logic

Can you compare object properties?


3. Java Collections Knowledge

Understanding:

  • List
  • Comparator
  • Streams
  • Optional

4. Algorithm Optimization

Can you improve:

Sorting O(n log n)

to

Single traversal O(n)

Real-World Applications

Payroll Systems

Find:

Highest paid employee

HR Applications

Generate reports:

Top salary employees

Salary ranking

Banking Systems

Find:

Highest account balance customer

E-Commerce

Find:

Most expensive product

Problem Statement

Given a list of employees, find the employee with the highest salary.


Employee Class Design

Employee contains:

id

name

department

salary

Employee Class

class Employee {


    private int id;


    private String name;


    private String department;


    private double salary;


    public Employee(
            int id,
            String name,
            String department,
            double salary) {


        this.id = id;

        this.name = name;

        this.department = department;

        this.salary = salary;

    }


    public int getId() {

        return id;

    }


    public String getName() {

        return name;

    }


    public String getDepartment() {

        return department;

    }


    public double getSalary() {

        return salary;

    }


    @Override
    public String toString() {


        return id +
                " " +
                name +
                " " +
                department +
                " " +
                salary;

    }

}

Input Example

Employees:

[
John IT 90000,

Alice HR 120000,

Bob Finance 75000
]

Expected Output

Alice HR 120000

Finding Maximum Salary Concept

The idea:

Maintain a variable:

highestSalaryEmployee

Start:

No employee selected

Compare each employee:

Current salary

>

Highest salary found

If true:

Update highest employee

Visualization

Input:

John 90000

Alice 120000

Bob 75000

Initial:

Highest = John

Compare Alice:

120000 > 90000

Update:

Highest = Alice

Compare Bob:

75000 > 120000

False.


Final:

Highest = Alice

Approach 1 — Sorting Employees by Salary

The first approach:

  1. Sort employees by salary.
  2. Return first or last employee.

Algorithm

Ascending sort:

Lowest salary

        ↓

Highest salary

Take:

Last element

Descending sort:

Highest salary

        ↓

Lowest salary

Take:

First element

Java Program — Sorting Approach

import java.util.*;

public class HighestSalarySorting {


    public static Employee findHighestSalary(
            List<Employee> employees) {


        employees.sort(
            Comparator.comparing(
                Employee::getSalary
            )
        );


        return employees.get(
                employees.size() - 1
        );

    }

}

Step-by-Step Explanation

Input:

John 90000

Alice 120000

Bob 75000

Before sorting:

90000

120000

75000

After sorting:

75000

90000

120000

Last element:

Alice 120000

Complexity Analysis — Sorting Approach

Sorting:

O(n log n)

Accessing last element:

O(1)

Total:

O(n log n)

Space:

O(1)

if sorting in place.


Advantages

  • Simple.
  • Easy to understand.
  • Useful when ranking is also required.

Drawbacks

  • Sorting all employees is unnecessary.
  • Slower than single traversal.
  • Modifies original list.

Approach 2 — Single Traversal Approach

The optimized solution:

Do not sort.

Simply scan once.


Algorithm

  1. Keep variable:
highestEmployee
  1. Traverse employees.
  2. Compare salaries.
  3. Update maximum.

Flow

Employee List

      ↓

Compare Salary

      ↓

Update Maximum

      ↓

Return Employee

Java Program — Single Pass

public class HighestSalarySinglePass {


    public static Employee findHighestSalary(
            List<Employee> employees) {


        Employee highest =
                null;


        for(Employee employee :
                employees) {


            if(highest == null
               ||
               employee.getSalary()
               >
               highest.getSalary()) {


                highest = employee;

            }

        }


        return highest;

    }

}

Dry Run

Input:

John 90000

Alice 120000

Bob 75000

Initial:

highest = null

First employee:

John

Update:

highest = John

Second employee:

Alice

Compare:

120000 > 90000

Update:

highest = Alice

Third employee:

Bob

Compare:

75000 > 120000

No update.


Final:

Alice 120000

Complexity Analysis — Single Pass

Each employee checked once.

Time:

O(n)

Space:

O(1)

Advantages

  • Most efficient.
  • No unnecessary sorting.
  • Production friendly.
  • Best interview solution.

Drawbacks

  • Slightly more logic.
  • Only finds maximum, not full ranking.

Finding Multiple Employees With Same Highest Salary

A common interview variation:

Find all employees who have the maximum salary.


Example

Employees:

John     120000

Alice    90000

Bob      120000

David    75000

Highest salary:

120000

Output:

John

Bob

Approach

Two-step approach:

Find Maximum Salary

        ↓

Filter Employees With Same Salary

Java Program

import java.util.*;

public class MultipleHighestSalary {


    public static List<Employee> findHighestPaidEmployees(
            List<Employee> employees) {


        double maxSalary =
                employees.stream()

                .mapToDouble(
                    Employee::getSalary
                )

                .max()

                .orElse(0);


        return employees.stream()

                .filter(
                    employee ->
                    employee.getSalary()
                    == maxSalary
                )

                .toList();

    }

}

Dry Run

Input:

John 120000

Alice 90000

Bob 120000

David 75000

Find maximum:

120000

Filter:

John → Match

Alice → No

Bob → Match

David → No

Result:

John

Bob

Finding Top K Highest Paid Employees

Another common interview problem:

Find top K employees with highest salaries.


Example:

Input:

John 90000

Alice 120000

Bob 75000

David 110000

Top 2:

Alice 120000

David 110000

Approach 1 — Sort and Limit

Algorithm:

Sort descending

       ↓

Take first K

Java Program

import java.util.*;

public class TopKSalaryEmployees {


    public static List<Employee> findTopK(
            List<Employee> employees,
            int k) {


        return employees.stream()

                .sorted(
                    Comparator
                    .comparing(
                        Employee::getSalary
                    )
                    .reversed()
                )

                .limit(k)

                .toList();

    }

}

Complexity Analysis

Sorting:

O(n log n)

Space:

O(n)

Better Approach for Large Data

For millions of employees:

Use:

PriorityQueue

Java Stream max() Method

Java Streams provide:

max()

for finding maximum values.


Syntax

stream.max(
    Comparator
)

Example

Optional<Employee> highest =
        employees.stream()

        .max(
            Comparator.comparing(
                Employee::getSalary
            )
        );

Complete Example

Optional<Employee> employee =

employees.stream()

.max(
    Comparator.comparing(
        Employee::getSalary
    )
);


employee.ifPresent(
    System.out::println
);

Why Optional?

Because the list may be empty.

Example:

[]

There is no employee.

Instead of returning:

null

Java returns:

Optional<Employee>

Handling Empty List

Example:

Employee result =
        employees.stream()

        .max(
            Comparator.comparing(
                Employee::getSalary
            )
        )

        .orElse(null);

Comparator Based Solution

Without streams:

Comparator<Employee> salaryComparator =
        Comparator.comparing(
            Employee::getSalary
        );


Employee highest =
        Collections.max(
            employees,
            salaryComparator
        );

Finding Highest Salary Per Department

Very common enterprise interview problem.

Example:

Employees:

John     IT       90000

Bob      IT      120000

Alice    HR       80000

David    HR      100000

Expected:

IT → Bob 120000

HR → David 100000

Approach

Group Employees

        ↓

Find Maximum Salary

        ↓

Return Employee

Java Program — groupingBy + maxBy

import java.util.*;
import java.util.stream.Collectors;


Map<String,Optional<Employee>> result =

employees.stream()

.collect(

Collectors.groupingBy(

Employee::getDepartment,


Collectors.maxBy(

Comparator.comparing(
    Employee::getSalary
)

)

)

);

Explanation

First:

Group:

IT

[
John,
Bob
]

Then:

Find maximum:

Bob 120000

Final:

IT → Bob

Sorting vs Single Pass Comparison

Approach Time Complexity Space Use Case
Sorting O(n log n) O(1) Need ranking
Single Pass O(n) O(1) Only maximum
Stream max() O(n) O(1) Modern Java
PriorityQueue O(n log k) O(k) Large data

Handling Null Values

Real applications may contain:

Employee = null

Salary = null

Null Employee Handling

Example:

employees.stream()

.filter(
    Objects::nonNull
)

.max(
    Comparator.comparing(
        Employee::getSalary
    )
);

Null Salary Handling

Use:

Comparator.comparing(
    Employee::getSalary,
    Comparator.nullsLast(
        Double::compare
    )
);

Custom Object Comparison

Java compares objects using:

Comparator

or

Comparable

Example:

Comparator<Employee> salaryComparator =
        Comparator.comparing(
            Employee::getSalary
        );

Comparable vs Comparator

Feature Comparable Comparator
Method compareTo() compare()
Location Inside class Separate object
Sorting Rules Single Multiple
Modification Required Not required

HashMap Approach for Department Salary

Another solution:

Store:

Department → Highest Salary Employee

Java Program

Map<String,Employee> highestByDepartment =
        new HashMap<>();


for(Employee employee : employees) {


    String dept =
            employee.getDepartment();


    if(!highestByDepartment.containsKey(dept)
       ||
       employee.getSalary()
       >
       highestByDepartment
       .get(dept)
       .getSalary()) {


        highestByDepartment.put(
            dept,
            employee
        );

    }

}

Dry Run

Input:

John IT 90000

Bob IT 120000

First:

IT → John

Second:

Compare:

120000 > 90000

Replace:

IT → Bob

Primitive vs Object Collections

Primitive:

double salary

Collections use:

Double

Autoboxing:

double

↓

Double

Common Interview Mistakes

Mistake 1

Sorting when only maximum is needed.

Problem:

O(n log n)

instead of:

O(n)

Mistake 2

Ignoring empty lists.

Wrong:

employees.get(0)

Correct:

Optional

Mistake 3

Using salary subtraction.

Wrong:

e1.salary - e2.salary

Correct:

Double.compare()

Mistake 4

Ignoring duplicate highest salaries.

Example:

John 100000

Bob 100000

Both should be returned.


Edge Cases

Case Handling
Empty list Return Optional.empty
One employee Return that employee
Same salary Return all matches
Null employee Filter null
Large data Use single pass

Interview Follow-up Questions

Q1. Find employee with highest salary.

Q2. Find second highest salary.

Q3. Find top K salaries.

Q4. Find highest salary per department.

Q5. Find employees with duplicate highest salaries.

Q6. Difference between max() and sorting.

Q7. How does Comparator work internally?


Related Java Collection Problems

  • Sort Employees by Salary
  • Group Employees by Department
  • Convert List to Map
  • Sort Map by Value
  • Find Duplicate Objects
  • Top K Frequent Elements

Key Takeaways

Finding highest salary follows:

Employee List

        ↓

Compare Salary

        ↓

Track Maximum

        ↓

Return Employee

Recommended solutions:

Only maximum needed

Use:

Single Traversal

or

Stream max()

Need ranking

Use:

Sorting

Need department-wise maximum

Use:

groupingBy()

+

maxBy()

Complexity:

Single pass:

Time: O(n)

Space: O(1)

Sorting:

Time: O(n log n)

Frequently Asked Interview Questions

Q1. What is the optimal solution?

A:

Single traversal because every employee only needs one comparison.


Q2. Why not sort?

Sorting does extra work when only maximum is required.


Q3. How to find highest salary per department?

Use:

groupingBy + maxBy

Q4. How to handle multiple employees with same salary?

Find max salary first, then filter employees.


Interview Tip

When asked:

"Find employee with highest salary in Java."

Explain:

  1. For simple maximum, use single traversal.
  2. For modern Java, use Stream max().
  3. For department-wise analysis, use groupingBy().
  4. Discuss duplicate salaries and empty lists.
  5. Mention sorting only when ranking is required.

For senior Java interviews, discuss:

  • Comparator design.
  • Stream operations.
  • Optional handling.
  • Performance trade-offs.
  • Enterprise reporting use cases.

This demonstrates strong understanding of Java Collections, Streams, object comparison, and efficient data processing.