Back to blog
Mar 13, 2026
4 min read

Calculate Special Bonus

Calculate the bonus for employees based on their ID and name, excluding those with odd IDs or names starting with 'M'.

Difficulty: Easy | Acceptance: 57.00% | Paid: No Topics: Database

Table: Employees

+-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_id is the primary key for this table. Each row of this table indicates the employee_id, name, and salary of an employee.

Write an SQL query to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the employee_id is an odd number and the name does not start with the character ‘M’. The bonus is 0 otherwise.

Return the result table ordered by employee_id.

The query result format is in the following example.

Examples

Example 1:

Input: Employees table: +-------------+----------+--------+ | employee_id | name | salary | +-------------+----------+--------+ | 2 | Meir | 3000 | | 3 | Michael | 4500 | | 7 | Addilyn | 2740 | | 8 | Juan | 6900 | | 9 | Kannon | 7000 | +-------------+----------+--------+ Output: +-------------+-------+ | employee_id | bonus | +-------------+-------+ | 2 | 0 | | 3 | 0 | | 7 | 0 | | 8 | 6900 | | 9 | 0 | +-------------+-------+ Explanation: The employees with IDs 2 and 3 get 0 bonus because their name starts with ‘M’. The employee with ID 7 gets 0 bonus because their employee_id is odd. The employee with ID 8 gets 6900 bonus because their employee_id is even and the name does not start with ‘M’. The employee with ID 9 gets 0 bonus because their employee_id is odd.

Constraints

1 <= employee_id <= 100
0 <= salary <= 100000
name consists only of lowercase and uppercase English letters.

Iterative Check

Intuition We iterate through the list of employees and check the conditions for each one individually. If the ID is odd or the name starts with ‘M’, the bonus is 0; otherwise, the bonus equals the salary.

Steps

  • Initialize an empty list for the results.
  • Loop through each employee in the input list.
  • Check if employee_id % 2 != 0 or if the name starts with ‘M’.
  • If either condition is true, append [employee_id, 0] to the results.
  • Otherwise, append [employee_id, salary] to the results.
  • Return the results list.
python
class Solution:
    def calculateSpecialBonus(self, employees: list) -&gt; list:
        result = []
        for emp in employees:
            eid, name, salary = emp[0], emp[1], emp[2]
            if eid % 2 != 0 or name.startswith('M'):
                result.append([eid, 0])
            else:
                result.append([eid, salary])
        return result

Complexity

  • Time: O(N) where N is the number of employees.
  • Space: O(N) to store the result list.
  • Notes: This approach is straightforward and easy to understand.

Functional Mapping

Intuition We use functional programming constructs like map, stream, or list comprehensions to transform the input list into the output list in a declarative way, applying the conditional logic inline.

Steps

  • Use a map function to iterate over the employees.
  • For each employee, apply the conditional logic: bonus = (id % 2 == 0 && name[0] != 'M') ? salary : 0.
  • Collect the transformed pairs into a new list.
  • Return the new list.
python
class Solution:
    def calculateSpecialBonus(self, employees: list) -&gt; list:
        return [[eid, 0 if eid % 2 != 0 or name.startswith('M') else salary] 
                for eid, name, salary in employees]

Complexity

  • Time: O(N) where N is the number of employees.
  • Space: O(N) to store the result list.
  • Notes: This approach is more concise and often preferred in modern codebases for simple transformations.