#577

Employee Bonus

Easy
DatabaseHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

We can efficiently retrieve the required information using a LEFT JOIN, which allows us to get all employees and their bonuses in one go. We can then filter the results based on the conditions.

⚙️

Algorithm

3 steps
  1. 1Step 1: Perform a LEFT JOIN between Employee and Bonus tables on empId.
  2. 2Step 2: Use COALESCE to handle null bonuses, treating them as null.
  3. 3Step 3: Filter results where the bonus is either less than 1000 or null.
solution.py5 lines
1# Full working Python code
2SELECT e.name, COALESCE(b.bonus, NULL) AS bonus
3FROM Employee e
4LEFT JOIN Bonus b ON e.empId = b.empId
5WHERE b.bonus < 1000 OR b.bonus IS NULL;

Complexity note: This complexity is linear because we are processing each employee and their corresponding bonus in a single pass.

  • 1Using LEFT JOIN ensures we capture all employees, even those without bonuses.
  • 2COALESCE is useful for handling null values effectively.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.