LLD Guide · Module 2: Problem prep · Lesson 02.02
Prep: Employee Management System (In-Memory)
CRUD looks trivial until you keep three indexes in step under concurrency, at sub-millisecond latency.
By Cruscible Engineering · Published
The hard part isn't storing employees. It's that one employee lives in several places at once, and every change has to move it in all of them together.
This lesson prepares you for Build an Employee Management System (In-Memory). It doesn't solve it.
1. What the employee management contract asks for
An in-memory store of employees with lookups by id, department, role and partial name, per-department salary statistics, and promotions. Nothing leaves the JVM: no database, Redis or files.
| Method | Success | Not found / empty |
|---|---|---|
addEmployee(name, dept, role, salary) | a new unique id | — |
getEmployee(id) | the employee map | null |
updateEmployee(id, dept, role, salary) | true; only non-null arguments change | false |
removeEmployee(id) | true | false |
getByDepartment / getByRole / getAllEmployees | a list of employee maps | an empty list, never null |
searchByName(query) | case-insensitive partial matches | an empty list |
getDepartmentStats(dept) | count, avgSalary, minSalary, maxSalary, totalSalary | count 0 and every salary 0.0 |
promoteEmployee(id, newRole, increase) | true; new role and salary + increase | false |
getEmployeeCount() | the number of employees | 0 |
Every employee map has exactly the keys id, name, department, role and salary, with salary as a Double. The statistics map's count is an Integer.
2. Lessons to read before the employee management
- 01.01 How LLD problems work: the performance part of the score, here with a high bar.
- 01.02 Thread-safety: especially "state spread over two structures". This problem is exactly that.
3. Employee management design decisions to make first
- What are your indexes? You need fast lookup by id, and lists by department and by role. Which structure backs each, and what do they store: whole employees, or ids?
- What moves when a department changes? Walk through
updateEmployee(id, "Sales", null, null)for someone in Engineering. Which indexes change, and in what order? - What makes
promoteEmployeeatomic? It reads the salary and writes a new one. What stops two promotions at the same moment from losing one raise? - How do you avoid one global lock? The performance target is 50,000 operations a second at under 1 ms P99. Which operations can run in parallel, and which must not overlap?
- What does a read return? If you hand out the employee object you store, can a caller change your data through it?
- How are ids generated so two threads never get the same one?
4. Employee management pitfalls
⚠️ Trap: a partial update must ignore
nullarguments.updateEmployee(id, null, null, 90000.0)changes only the salary, and it still returnstrue.
⚠️ Trap: statistics for an empty or unknown department are zeros (
0and0.0), notnulland notNaNfrom dividing by zero.
⚠️ Trap: an employee who changes department or role must disappear from the old index and appear in the new one. A stale index entry shows up in the wrong list.
⚠️ Trap: name search is case-insensitive and partial:
"ali"finds"Alice".
💡 Tip: you may skip the injected KV store and use your own collections.
💡 Tip: return copies (new maps) from every read. Then no caller can change your stored data, and no reader sees a half-finished update.
Before you submit
- Ids are unique, including when many threads add at once
getEmployeereturnsnullfor an unknown id; every list method returns an empty list, nevernull- Employee maps have exactly
id,name,department,roleandsalary, withsalaryas aDouble updateEmployeeapplies only the non-null arguments and returnsfalsefor an unknown id- Changing department or role moves the employee between indexes
removeEmployeetakes the employee out of every index- Department statistics are correct, and all zeros for an empty department
promoteEmployeechanges role and salary together, and concurrent promotions don't lose an increasesearchByNameis case-insensitive and matches partial names- No single global lock serializes every operation
Further reading
After you've tried it
Submit first. The full walkthrough of one solution: Building an Employee Management System in Pure Java.