Table: Employees
+-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_id is the primary key (column with unique values) for this table. Each row of this table indicates the employee ID, employee name, and salary.
Write a solution to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee's name does not start with the character 'M'. The bonus of an employee is 0 otherwise.
Return the result table ordered by employee_id.
The result format is in the following example.
Example 1:
Input: Employees table: +-------------+---------+--------+ | employee_id | name | salary | +-------------+---------+--------+ | 2 | Meir | 3000 | | 3 | Michael | 3800 | | 7 | Addilyn | 7400 | | 8 | Juan | 6100 | | 9 | Kannon | 7700 | +-------------+---------+--------+ Output: +-------------+-------+ | employee_id | bonus | +-------------+-------+ | 2 | 0 | | 3 | 0 | | 7 | 7400 | | 8 | 0 | | 9 | 7700 | +-------------+-------+ Explanation: The employees with IDs 2 and 8 get 0 bonus because they have an even employee_id. The employee with ID 3 gets 0 bonus because their name starts with 'M'. The rest of the employees get a 100% bonus.
This approach involves iterating through each employee, checking if their employee_id is odd and their name does not start with 'M'. If both conditions are met, the bonus is set to their full salary; otherwise, it is set to 0. Loop through each employee and apply these checks systematically.
This C program defines a struct for employees and then checks each employee's ID and name. If the ID is odd and name doesn't start with 'M', it assigns the employee's salary as the bonus. Results are stored in an array and printed.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
This approach utilizes functional programming paradigms such as map/filter. It maps through each row of data, filters the rows based on the bonus eligibility criteria, and transforms them with the mapped values.
This Python solution uses lambda functions and 'map' to apply the bonus logic across all employees in a functional style approach without using an explicit loop. It's concise and leverages built-in Python capabilities.
JavaScript
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(n), for storing the results.
| Approach | Complexity |
|---|---|
| Approach 1: Using Conditional Logic | Time Complexity: O(n), where n is the number of employees. |
| Approach 2: Functional Programming Style | Time Complexity: O(n), where n is the number of employees. |
LeetCode 1873 Interview SQL Question with Detailed Explanation | Practice SQL • Everyday Data Science • 5,483 views views
Watch 9 more video solutions →Practice Calculate Special Bonus with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor