Home › Interview Questions › What happens if two employees have the same second…

What happens if two employees have the same second highest salary?

🟢 Easy Conceptual Fresher level
2Times asked
Nov 2026Last seen
Nov 2026First seen

💡 Model Answer

Finding the second highest salary in a list of employees is a common interview problem. A simple approach is to iterate through the list while keeping track of the highest and second highest distinct salaries. When you encounter a salary greater than the current highest, you shift the current highest to second highest and update highest. If the salary equals the current highest, you skip it because you want distinct values. If the salary is between the highest and second highest, you update second highest. After the loop, the second highest variable holds the desired value. If two employees share that second highest salary, you can return both employees or simply return the salary value. In many business contexts, you might need to list all employees who tie for the second highest salary, so you would collect them in a list during the iteration. The algorithm runs in O(n) time and O(1) extra space. If you need to handle ties differently, you can add a secondary criterion such as hire date or employee ID to break ties deterministically.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500