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 NameType
company_idint
employee_idint
employee_namevarchar
salaryint

โš ๏ธ 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
Corporate Tax Calculator WorkflowStep 1: GroupOrganize bycompany_idโœ“Step 2: Find MaxMAX(salary) percompanyโœ“Step 3: Tax Bracket<1000: 0%1000-10000: 24%>10000: 49%โœ“Step 4: Apply Taxsalary ร— (1-rate)ROUND resultโœ“Example: Company AEmployees: Alice($8000), Bob($12000), Carol($6000)Max Salary: $12000 โ†’ 49% tax rateAfter-tax salaries:โ€ข Alice: $8000 ร— 0.51 = $4080โ€ข Bob: $12000 ร— 0.51 = $6120โ€ข Carol: $6000 ร— 0.51 = $3060๐ŸŽฏ O(n) time complexity with SQL window functions!
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

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

No additional space needed beyond the result set

n
2n
โœ“ 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
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28 Apple 22
26.8K Views
Medium-High Frequency
~18 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