Field-Level Visibility Filter for Employee Records
Data Structure DesignAuto-graded · 23 test casesLast updated
Build a function that returns only the employee record fields a viewer may see, based on admin status, identity, department, reporting line, and view type.
Field-Level Visibility Filter
An HR system stores a record for every employee. Before a record is sent to a caller, each field must be checked against the caller's permissions. Write a function that returns a new dictionary holding only the fields the viewer is allowed to see.
Facts about the viewer and the target
Compare the viewer with the target employee to derive four facts:
- admin: the viewer's
roleis exactly"ADMIN". - self: the viewer's
idequals the target'sid. - same department: the viewer's
departmentequals the target'sdepartment. - direct manager: the viewer's
idequals the target'smanager_id. Only this id comparison matters. A viewer whose role is"MANAGER"but whose id differs frommanager_idis not a direct manager. A viewer whose id matchesmanager_idis a direct manager, whatever their role string says.
Visibility rules
| Field | Visible when |
|---|---|
id, name, department | always |
email | admin, or self, or same department |
phone | admin, or self, or direct manager |
salary, ssn | the context is "detail" and (admin or direct manager) |
Notes on salary and ssn:
- In the
"list"context these two fields are never included, not even for an admin. - Being self does not grant them. A non-admin viewing their own record never sees
salaryorssn. An admin viewing their own record in"detail"context does, because they are an admin. - Any other roles or strings give no extra rights beyond the facts above.
Output
Return a dictionary containing only the permitted fields from id, name, department, email, phone, salary, ssn, with values copied from target_user. Fields that are not permitted are left out entirely, not set to None. manager_id is never part of the output. Do not modify target_user or viewer.
Input
viewer:{"id": str, "role": str, "department": str}target_user:{"id": str, "name": str, "department": str, "manager_id": str, "email": str, "phone": str, "salary": int, "ssn": str}context:"detail"or"list"(defaults to"detail").
Constraints
- All the keys listed above are always present in
viewerandtarget_user. contextis always either"detail"or"list".- The returned dictionary must be a new object, and the inputs must remain unchanged.
Examples
Example 1
Input: filter_employee_record({'id': 'u2', 'role': 'EMPLOYEE', 'department': 'Engineering'}, {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'manager_id': 'u3', 'email': '[email protected]', 'phone': '555-0100', 'salary': 120000, 'ssn': '000-00-0000'}, 'detail')
Output: {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'email': '[email protected]'}
Example 2
Input: filter_employee_record({'id': 'admin', 'role': 'ADMIN', 'department': 'Finance'}, {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'manager_id': 'u3', 'email': '[email protected]', 'phone': '555-0100', 'salary': 120000, 'ssn': '000-00-0000'}, 'detail')
Output: {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'email': '[email protected]', 'phone': '555-0100', 'salary': 120000, 'ssn': '000-00-0000'}
Example 3
Input: filter_employee_record({'id': 'u1', 'role': 'EMPLOYEE', 'department': 'Engineering'}, {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'manager_id': 'u3', 'email': '[email protected]', 'phone': '555-0100', 'salary': 120000, 'ssn': '000-00-0000'}, 'list')
Output: {'id': 'u1', 'name': 'Alice', 'department': 'Engineering', 'email': '[email protected]', 'phone': '555-0100'}
Hints
Hint 1
Compute the four relationship facts (admin, self, same department, direct manager) once at the top, then reuse them for every field.
Hint 2
Build the output by adding fields only when the rule allows them, instead of copying everything and deleting. This way `manager_id` and any unlisted field stay hidden automatically.
Hint 3
For `salary` and `ssn`, check the context first. If it is `"list"`, skip them entirely. Otherwise require admin or direct manager. Do not let self qualify.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Split the work into two steps.
- Derive the relationship facts once. Four booleans describe how the viewer relates to the target:
is_admin(role is"ADMIN"),is_self(ids match),is_same_dept(departments match), andis_direct_manager(viewer id equals the target'smanager_id). - Add each field only if its rule passes. Start with the always-visible fields (
id,name,department). Then addemail,phone, and finallysalaryandssnaccording to the table in the statement. The output is built from scratch, so nothing is ever mutated.
Why it is correct
Each rule in the statement corresponds to exactly one condition:
email:admin or self or same_deptphone:admin or self or direct_managersalaryandssn:context == "detail" and (admin or direct_manager)
The last condition has no self term, so a non-admin can never see their own salary or ssn. The context test sits in front of the role test, so "list" hides these two fields even from admins. The direct-manager check compares ids rather than the role string, so a "MANAGER" who does not manage the target gets no extra access. Because fields are added only when allowed (an allow-list), manager_id and any other unlisted key never reach the output.
Complexity
- Time: O(1). There is a fixed number of fields and a fixed number of comparisons.
- Space: O(1) for the output dictionary, which holds at most seven entries.
Reference solution
def filter_employee_record(viewer: dict, target_user: dict, context: str = "detail") -> dict:
is_admin = viewer["role"] == "ADMIN"
is_self = viewer["id"] == target_user["id"]
is_same_dept = viewer["department"] == target_user["department"]
is_direct_manager = viewer["id"] == target_user["manager_id"]
result = {
"id": target_user["id"],
"name": target_user["name"],
"department": target_user["department"],
}
if is_admin or is_self or is_same_dept:
result["email"] = target_user["email"]
if is_admin or is_self or is_direct_manager:
result["phone"] = target_user["phone"]
if context == "detail" and (is_admin or is_direct_manager):
result["salary"] = target_user["salary"]
result["ssn"] = target_user["ssn"]
return result