Calculate Salaries - Problem
๐ข You're working as a payroll specialist for a multi-company corporation! Each company has different tax brackets based on their highest-paid employee's salary.
Your Mission: Calculate the after-tax salaries for all employees across different companies, where the tax rate is determined by each company's maximum salary.
๐ Tax Brackets:
- 0% tax if company's max salary < $1,000
- 24% tax if company's max salary is between $1,000-$10,000 (inclusive)
- 49% tax if company's max salary > $10,000
Database Schema:
| Column Name | Type |
|---|---|
| company_id | int |
| employee_id | int |
| employee_name | varchar |
| salary | int |
โ ๏ธ Important: Round all final salaries to the nearest integer and return results in any order.
Input & Output
basic_example.sql โ SQL
$
Input:
Salaries table:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 1 | 1 | Tony | 2000 |
| 1 | 2 | Pronub | 21300 |
| 1 | 3 | Tyrrox | 10800 |
| 2 | 1 | Pam | 300 |
| 2 | 7 | Bassem | 450 |
| 2 | 9 | Hermione | 700 |
โบ
Output:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 1 | 1 | Tony | 1020 |
| 1 | 2 | Pronub | 10863 |
| 1 | 3 | Tyrrox | 5508 |
| 2 | 1 | Pam | 300 |
| 2 | 7 | Bassem | 450 |
| 2 | 9 | Hermione | 700 |
๐ก Note:
Company 1: Max salary is 21300 (>10000), so 49% tax rate applies. Tony: 2000ร0.51=1020, Pronub: 21300ร0.51โ10863, Tyrrox: 10800ร0.51=5508. Company 2: Max salary is 700 (<1000), so 0% tax rate applies - all salaries remain unchanged.
mid_tier_tax.sql โ SQL
$
Input:
Salaries table:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 3 | 1 | Alice | 5000 |
| 3 | 2 | Bob | 8000 |
| 3 | 3 | Carol | 6500 |
โบ
Output:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 3 | 1 | Alice | 3800 |
| 3 | 2 | Bob | 6080 |
| 3 | 3 | Carol | 4940 |
๐ก Note:
Company 3: Max salary is 8000 (between 1000-10000), so 24% tax rate applies. Alice: 5000ร0.76=3800, Bob: 8000ร0.76=6080, Carol: 6500ร0.76=4940.
edge_case_boundary.sql โ SQL
$
Input:
Salaries table:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 4 | 1 | Dave | 1000 |
| 4 | 2 | Eve | 900 |
| 5 | 1 | Frank | 10000 |
| 5 | 2 | Grace | 5000 |
โบ
Output:
| company_id | employee_id | employee_name | salary |
|------------|-------------|---------------|--------|
| 4 | 1 | Dave | 760 |
| 4 | 2 | Eve | 684 |
| 5 | 1 | Frank | 7600 |
| 5 | 2 | Grace | 3800 |
๐ก Note:
Company 4: Max salary is exactly 1000 (boundary case), so 24% tax applies. Company 5: Max salary is exactly 10000 (boundary case), so 24% tax applies. Both companies fall within the [1000,10000] inclusive range.
Visualization
Tap to expand
Understanding the Visualization
1
Group by Company
Organize employees by their company_id to process each company separately
2
Find Company Max
For each company, identify the highest-paid employee's salary
3
Determine Tax Bracket
Based on company max: <1000โ0%, 1000-10000โ24%, >10000โ49%
4
Apply Tax Rate
Multiply each employee's salary by (1 - tax_rate) and round to nearest integer
Key Takeaway
๐ฏ Key Insight: Tax rates are determined at the company level based on the highest earner, then applied uniformly to all employees within that company. Window functions make this calculation efficient by avoiding repeated scans of the data.
Time & Space Complexity
Time Complexity
O(nยฒ)
For each of n employees, we scan all employees in the same company
โ Quadratic Growth
Space Complexity
O(1)
No additional space needed beyond the result set
โ Linear Space
Constraints
- 1 โค employees count โค 104
- 1 โค company_id, employee_id โค 104
- 1 โค employee_name.length โค 20
- 1 โค salary โค 106
- (company_id, employee_id) is unique
- Round salary to nearest integer
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code