Highest Salary Using Java Streams
Company: Wells Fargo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Highest Salary Using Java Streams
You are given a list of `Employee` objects. Each `Employee` is a plain Java object (POJO) with the following fields:
```java
public class Employee {
private String name; // employee's full name, never null
private String department; // department name, never null
private long salary; // annual salary in whole dollars, always >= 0
// standard constructor + getters (getName(), getDepartment(), getSalary())
}
```
Using **only the Java Stream API** (no explicit `for`/`while` loops, no manual mutation of an accumulator variable, no sorting of the whole list into a temporary collection), compute the **highest salary** among all employees in the list.
Return the result as an `OptionalLong` (or `Optional<Long>`): if the input list is empty, the result must be **empty**; otherwise it must contain the maximum salary.
### Function Signature
```java
public OptionalLong maxSalary(List<Employee> employees)
```
### Examples
Example 1:
```
Input: [ {"Ana", "Sales", 90000}, {"Bo", "Eng", 145000}, {"Cy", "Eng", 120000} ]
Output: OptionalLong[145000]
```
Example 2:
```
Input: [ {"Dee", "HR", 60000} ]
Output: OptionalLong[60000]
```
Example 3 (empty list):
```
Input: []
Output: OptionalLong.empty
```
### Constraints
- `0 <= employees.size() <= 10^5`
- `0 <= salary <= 10^9` (fits in a `long`)
- The list itself is never `null`, and no element is `null`.
- Solve it with a single stream pipeline; do not use an explicit loop or an external mutable accumulator.
Quick Answer: This question tests a software engineer's practical proficiency with the Java Stream API, specifically the ability to perform aggregate operations on collections without imperative loops. It evaluates functional programming fluency and proper handling of edge cases using Optional types, skills commonly assessed in Java-focused coding interviews.
You are given a list of employee salaries (annual salary in whole dollars, each >= 0). Compute the highest salary in the list.
Return the maximum salary if the list is non-empty; if the list is empty, return an empty result (in Python: None; in Java this is naturally modeled as OptionalLong.empty).
The original Wells Fargo interview phrasing asks you to do this over a List<Employee> POJO using only the Java Stream API (no explicit for/while loops, no external mutable accumulator, no sorting a temporary collection) — a single pipeline such as employees.stream().mapToLong(Employee::getSalary).max(). Here you operate directly on the extracted salary list so the logic is identical and language-agnostic.
Function signature (Java, original): public OptionalLong maxSalary(List<Employee> employees)
Examples:
maxSalary([90000, 145000, 120000]) -> 145000
maxSalary([60000]) -> 60000
maxSalary([]) -> empty / None
Constraints
- 0 <= number of salaries <= 10^5
- 0 <= salary <= 10^9 (fits in a 64-bit long)
- The list itself is never null and contains no null elements
- Solve with a single stream pipeline / reduction — no explicit loop or external mutable accumulator
Examples
Input: ([90000, 145000, 120000],)
Expected Output: 145000
Explanation: The maximum of the three salaries is 145000.
Input: ([60000],)
Expected Output: 60000
Explanation: A single-element list — the max is that element.
Input: ([],)
Expected Output: None
Explanation: Empty list has no maximum, so return an empty result (None).
Input: ([0, 0, 0],)
Expected Output: 0
Explanation: All salaries are zero; the maximum is 0 (boundary value).
Input: ([5, 5, 3, 5],)
Expected Output: 5
Explanation: Duplicate maximum values are handled correctly; the max is 5.
Input: ([1000000000, 999999999, 1],)
Expected Output: 1000000000
Explanation: Large values near the 10^9 upper bound; the max is 1000000000.
Hints
- The whole task is a single reduction: find the maximum over the salaries.
- Handle the empty list as a distinct case — there is no maximum, so return an empty result (None / OptionalLong.empty / null).
- In Java, employees.stream().mapToLong(Employee::getSalary).max() already returns an OptionalLong, so you get the empty case for free.
- Do not sort the list (O(n log n)) just to read the last element — a linear max reduction is O(n).