Replace Employee ID With The Unique Identifier - Problem

You're working at a company that's migrating from an old employee ID system to a new unique identifier system. Your task is to map employee information from the old system to the new one.

You have two tables:

  • Employees: Contains employee id and name
  • EmployeeUNI: Contains the mapping between old id and new unique_id

Your goal is to create a report that shows each employee's unique_id and name. If an employee doesn't have a unique_id in the new system yet, show null for their unique_id.

This is a classic LEFT JOIN scenario where we want all employees, whether they have unique IDs or not.

Input & Output

example_1.sql โ€” Basic Example
$ Input: Employees table: | id | name | |----|-------| | 1 | Alice | | 7 | Bob | | 11 | Meir | EmployeeUNI table: | id | unique_id | |----|----------| | 1 | 101 | | 7 | 102 |
โ€บ Output: | unique_id | name | |-----------|-------| | 101 | Alice | | 102 | Bob | | null | Meir |
๐Ÿ’ก Note: Alice (ID=1) maps to unique_id 101, Bob (ID=7) maps to unique_id 102, and Meir (ID=11) has no mapping so gets null
example_2.sql โ€” All Employees Have Unique IDs
$ Input: Employees table: | id | name | |----|------| | 1 | John | | 2 | Jane | EmployeeUNI table: | id | unique_id | |----|----------| | 1 | 201 | | 2 | 202 |
โ€บ Output: | unique_id | name | |-----------|------| | 201 | John | | 202 | Jane |
๐Ÿ’ก Note: All employees have corresponding unique IDs, so no null values in result
example_3.sql โ€” No Employees Have Unique IDs
$ Input: Employees table: | id | name | |----|-------| | 1 | Alice | | 2 | Bob | EmployeeUNI table: | id | unique_id | |----|----------| (empty)
โ€บ Output: | unique_id | name | |-----------|-------| | null | Alice | | null | Bob |
๐Ÿ’ก Note: No employees have unique IDs yet, so all get null values while preserving their names

Visualization

Tap to expand
All Employees(Must be preserved)Has UniqueID(Optional match)IntersectionEmployees WITHUnique IDsResult: All Employees + Their Unique IDs (if available)With Unique ID101 | Alice102 | BobWithout Unique IDnull | Meirnull | JohnLEFT JOIN MagicPreserves ALL employeesAdds unique_id when availableLEFT JOIN
Understanding the Visualization
1
All Members Preserved
LEFT JOIN ensures every employee appears in results
2
Match Where Possible
When unique_id exists, include it in the result
3
Fill Gaps with NULL
When no unique_id exists, use null placeholder
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN is perfect when you need ALL records from one table, with optional matching data from another table

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through both tables with hash join optimization

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for hash table and result set

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค employees.length โ‰ค 1000
  • 1 โ‰ค employeeUNI.length โ‰ค 1000
  • 1 โ‰ค id โ‰ค 106
  • Each row in EmployeeUNI has unique (id, unique_id) pairs
  • Employee names contain only English letters and spaces
Asked in
Amazon 23 Microsoft 18 Google 15 Meta 12
52.0K Views
High Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen