Hone logo
Hone
Problems

Swap Salary

You are tasked with creating a program that can update employee salary records. Specifically, you need to implement a feature that allows for a conditional salary swap. Given a table of employees and their salaries, you will adjust the salaries based on a specific rule. This is a common data manipulation task that is fundamental in database management and application development.

Problem Description

You will be given a table named Employees with two columns:

  • id: An integer representing the unique identifier for each employee.
  • salary: A decimal number representing the employee's salary.

Your task is to update the salary column such that all employees with an odd id have their salary multiplied by 2, and all employees with an even id have their salary divided by 2.

Key Requirements:

  • The update should be applied to all rows in the Employees table.
  • The id column should remain unchanged.
  • The salary column will be modified based on the parity of the id.

Expected Behavior:

  • If an employee's id is odd, their salary should become salary * 2.
  • If an employee's id is even, their salary should become salary / 2.

Edge Cases:

  • What if the salary is 0? (It should remain 0 after the operation).
  • What if the salary is negative? (The operation should still apply, e.g., -10 / 2 = -5).

Examples

Example 1:

Input: Employees table:

idsalary
110000
220000
330000
440000

Output: Employees table:

idsalary
120000
210000
360000
420000

Explanation:

  • Employee with id 1 (odd) has salary 10000 -> 10000 * 2 = 20000.
  • Employee with id 2 (even) has salary 20000 -> 20000 / 2 = 10000.
  • Employee with id 3 (odd) has salary 30000 -> 30000 * 2 = 60000.
  • Employee with id 4 (even) has salary 40000 -> 40000 / 2 = 20000.

Example 2:

Input: Employees table:

idsalary
55000
66000

Output: Employees table:

idsalary
510000
63000

Explanation:

  • Employee with id 5 (odd) has salary 5000 -> 5000 * 2 = 10000.
  • Employee with id 6 (even) has salary 6000 -> 6000 / 2 = 3000.

Example 3 (Edge Case):

Input: Employees table:

idsalary
70
8-1000

Output: Employees table:

idsalary
70
8-500

Explanation:

  • Employee with id 7 (odd) has salary 0 -> 0 * 2 = 0.
  • Employee with id 8 (even) has salary -1000 -> -1000 / 2 = -500.

Constraints

  • The id column is an integer and is guaranteed to be unique for each employee.
  • The salary column is a decimal number (e.g., float, double, decimal type) and can be positive, zero, or negative.
  • The number of employees in the table can range from 1 to 1000.
  • The values in the salary column can range from -1,000,000 to 1,000,000.
  • The solution should be efficient, aiming for a single pass or minimal operations over the table.

Notes

Consider how to check for the parity (even or odd) of an integer. A common way is to use the modulo operator.

Pseudocode Hint: You can use a conditional statement (IF-THEN-ELSE) to determine which operation to perform on the salary based on the id.

Loading editor...
plaintext