Domain 1EasyCORE

Replace Employee ID With The Unique Identifier

The problem requires us to replace employee IDs in a given table with unique identifiers. The inputs are two tables: `Employees` and `EmployeeUniqueness`. The `Employees` table contains columns for `employee_id`, `name`, and other attributes, while the `EmployeeUniqueness` table contains columns for `employee_id` and `unique_identifier`. The output should be a modified `Employees` table where each `employee_id` is replaced with its corresponding `unique_identifier`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

1378. Replace Employee ID With The Unique Identifier

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 90.3%


Problem Breakdown

The problem requires us to replace employee IDs in a given table with unique identifiers. The inputs are two tables: Employees and EmployeeUniqueness. The Employees table contains columns for employee_id, name, and other attributes, while the EmployeeUniqueness table contains columns for employee_id and unique_identifier. The output should be a modified Employees table where each employee_id is replaced with its corresponding unique_identifier.

Inputs:

  • Employees table:
    • employee_id
    • name
    • Other attributes
  • EmployeeUniqueness table:
    • employee_id
    • unique_identifier

Outputs:

  • Modified Employees table with employee_id replaced by unique_identifier.

Implicit Constraints/Edge Cases:

  1. Empty Input: If either of the tables is empty, the output should be an empty table.
  2. Duplicates: There might be duplicate employee_ids in the Employees table, but each employee_id should map to a unique unique_identifier.
  3. Missing IDs: Some employee_ids in the Employees table might not have a corresponding entry in the EmployeeUniqueness table.

Intuition

The brute-force approach would involve iterating through each row of the Employees table, searching for the corresponding unique_identifier in the EmployeeUniqueness table, and replacing it. This approach is inefficient with a time complexity of O(n * m), where n is the number of rows in the Employees table and m is the number of rows in the EmployeeUniqueness table.

A better approach involves using a hash map (dictionary) to store the mapping from employee_id to unique_identifier. This allows for constant-time lookups, reducing the time complexity to O(n + m).

Approach

  1. Create a Mapping: Iterate through the EmployeeUniqueness table and create a dictionary that maps each employee_id to its corresponding unique_identifier.
  2. Replace IDs: Iterate through the Employees table and replace each employee_id with its corresponding unique_identifier from the dictionary.
  3. Handle Missing IDs: If an employee_id in the Employees table does not exist in the dictionary, it can be left unchanged or handled as per specific requirements (e.g., setting to a default value).

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of rows in the Employees table and m is the number of rows in the EmployeeUniqueness table. This is because we iterate through both tables once.
  • Space Complexity: O(m) for storing the dictionary, where m is the number of unique employee IDs.

Solution

def replace_employee_id(employees, employee_uniqueness):
    # Step 1: Create a mapping from employee_id to unique_identifier
    id_map = {row['employee_id']: row['unique_identifier'] for row in employee_uniqueness}
    
    # Step 2: Replace employee_id with unique_identifier in the Employees table
    for emp in employees:
        if emp['employee_id'] in id_map:
            emp['employee_id'] = id_map[emp['employee_id']]
    
    return employees

# Example usage:
employees = [
    {'employee_id': 1, 'name': 'Alice'},
    {'employee_id': 2, 'name': 'Bob'}
]

employee_uniqueness = [
    {'employee_id': 1, 'unique_identifier': 'UID001'},
    {'employee_id': 2, 'unique_identifier': 'UID002'}
]

modified_employees = replace_employee_id(employees, employee_uniqueness)
print(modified_employees)

Edge Cases Handled

  • Empty Input: If either employees or employee_uniqueness is empty, the function will return an empty list.
  • Duplicates: The dictionary ensures that each employee_id maps to a unique unique_identifier.
  • Missing IDs: Any employee_id in employees that does not have a corresponding entry in id_map remains unchanged.