Highest Salaries Difference - Problem

Given a Salaries table with employee names, departments, and salaries, find the absolute difference between the highest salary in the marketing department and the highest salary in the engineering department.

The table contains:

  • emp_name (varchar): Employee name
  • department (varchar): Department name
  • salary (int): Employee salary

Return a single value representing the absolute difference between these two maximum salaries.

Table Schema

Salaries
Column Name Type Description
emp_name PK varchar Employee name
department PK varchar Department name (marketing, engineering, etc.)
salary int Employee salary
Primary Key: (emp_name, department)
Note: Each row represents an employee's salary in a specific department. At least one employee exists in both marketing and engineering departments.

Input & Output

Example 1 — Basic Department Comparison
Input Table:
emp_name department salary
Alice marketing 80000
Bob marketing 90000
Charlie engineering 85000
David engineering 92000
Output:
salary_difference
2000
💡 Note:

Marketing department max salary is 90000 (Bob), engineering department max salary is 92000 (David). The absolute difference is |90000 - 92000| = 2000.

Example 2 — Marketing Higher
Input Table:
emp_name department salary
John marketing 95000
Sarah marketing 88000
Mike engineering 87000
Lisa engineering 79000
Output:
salary_difference
8000
💡 Note:

Marketing department max salary is 95000 (John), engineering department max salary is 87000 (Mike). The absolute difference is |95000 - 87000| = 8000.

Constraints

  • 1 ≤ emp_name.length ≤ 20
  • 1 ≤ department.length ≤ 20
  • 1000 ≤ salary ≤ 200000
  • At least one employee exists in both marketing and engineering departments

Visualization

Tap to expand
Highest Salaries Difference INPUT Name Dept Salary Alice Marketing 85000 Bob Engineering 95000 Carol Marketing 92000 Dave Engineering 120000 Eve Marketing 78000 Frank Engineering 105000 Marketing Engineering Goal: Find |MAX(mkt) - MAX(eng)| ALGORITHM STEPS 1 Filter Marketing WHERE dept = 'Marketing' 85000 92000 [MAX] 78000 2 Filter Engineering WHERE dept = 'Engineering' 95000 120000 [MAX] 105000 3 Calculate Difference ABS(max_mkt - max_eng) 4 Return Result |92000 - 120000| = 28000 Calculation 92000 - 120000 -28000 ABS = 28000 FINAL RESULT Marketing 92000 Engineering 120000 28000 OUTPUT 28000 OK - Complete Key Insight: Use SQL subqueries or CASE WHEN to calculate MAX salary per department, then compute the absolute difference. The ABS() function ensures the result is always positive regardless of which department has the higher maximum salary. Time: O(n), Space: O(1) for aggregation. TutorialsPoint - Highest Salaries Difference | Optimal Solution
Asked in
Amazon 15 Microsoft 12 Facebook 8
26.8K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen