KatInterview

Field-Level Visibility Filter for Employee Records

Data Structure DesignAuto-graded · 23 test cases
EasyAuto-gradedData Structure Designaccess-controldictionariessimulation

Last 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 role is exactly "ADMIN".
  • self: the viewer's id equals the target's id.
  • same department: the viewer's department equals the target's department.
  • direct manager: the viewer's id equals the target's manager_id. Only this id comparison matters. A viewer whose role is "MANAGER" but whose id differs from manager_id is not a direct manager. A viewer whose id matches manager_id is a direct manager, whatever their role string says.

Visibility rules

FieldVisible when
id, name, departmentalways
emailadmin, or self, or same department
phoneadmin, or self, or direct manager
salary, ssnthe 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 salary or ssn. 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 viewer and target_user.
  • context is 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.