The 30 second answer
I map salaries, call distinct so ties at the top do not hide the real second place, sort descending, skip one, and take findFirst. That returns an Optional, which stays empty if there are fewer than two distinct salaries. Sorting is fine for interviews; for large data I would track the top two in one pass instead.
Optional<Double> second = emps.stream()
.map(Employee::salary)
.distinct()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst();distinct() matters: without it, two people tied for the top salary make "second" equal to the first. Return Optional because fewer than two distinct salaries is possible.
Complexity: O(n log n) time and O(n) space for the sort. [Opinion] A single pass that tracks the top two values is O(n) time and O(1) space — mention that trade-off.
Tip: state time and space; clarify whether "second highest" means distinct values or the second employee.
Sources: