Design an EmployeeManager class with add, get, remove and average salary backed by a dict for O(1) lookups. Practical OOP design, tested in Python.
The problem
Implement EmployeeManager:
add_employee(name, salary)— add or updateget_employee(name)— return the salary, orNoneremove_employee(name)— returnTrueif removed,Falseif absentaverage_salary()— average of all salaries,0if there are none
Examples
Example 1
Input
run_ops(EmployeeManager, [], [["add_employee", "John", 50000], ["add_employee", "Alice", 70000], ["get_employee", "John"]])
Expected output
[None, None, 50000]
Example 2
Input
run_ops(EmployeeManager, [], [["add_employee", "A", 100], ["add_employee", "B", 300], ["average_salary"]])
Expected output
[None, None, 200.0]
+ 2 hidden tests on Submit.
Edge cases to ask about
- Empty manager
- Removing a missing employee
- Updating an existing employee
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.
Pick both to reveal the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Dict keyed by name | O(1) per lookup, O(n) average | O(n) | Keep a running total to make average O(1). |
Walkthrough of the optimal approach (try it yourself first)
A dict gives O(1) add, get and remove. average_salary is O(n) as written; if it is called often, keep a running total updated on add/remove to make it O(1) — a classic design trade-off to mention.
Complexity: O(1) time, O(n) space. Add/get/remove are O(1) dict operations; average is O(n) unless you maintain a running total.
Reveal the reference solution
class EmployeeManager: def __init__(self): self._salaries = {} def add_employee(self, name, salary): self._salaries[name] = salary def get_employee(self, name): return self._salaries.get(name) def remove_employee(self, name): return self._salaries.pop(name, None) is not None def average_salary(self): if not self._salaries: return 0 return sum(self._salaries.values()) / len(self._salaries)
Follow-ups interviewers ask
- Make average O(1).
- Return the top 3 earners efficiently.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Employee Management System Class in Python?
The optimal solution runs in O(1) time and O(n) auxiliary space. Add/get/remove are O(1) dict operations; average is O(n) unless you maintain a running total.
What follow-up questions do interviewers ask about Employee Management System Class?
Make average O(1). Return the top 3 earners efficiently.
