Skip to main content
All questions
HardGraphs · DAG Traversal

Find the Admin User

Written by a community member · · 1 attempt · 100% pass

0

Given a list permissions of triples [entityId, relationship, objectId], return the ID of the unique admin user, who has access to every camera in the system.

There are two relationship types:

  • cameraOwner: a user directly owns the camera, or a group owns the camera and grants it to members of that group.
  • groupMember: a user belongs to a group, or the first group is nested inside the second group. A member of a nested group inherits permissions from its containing group. Membership chains may be arbitrarily deep, but contain no cycles. A user may belong to multiple groups.

The camera universe consists of every objectId appearing in a cameraOwner entry. A user's access includes cameras they own directly and cameras owned by any group they belong to, directly or through nested membership. Exactly one user has access to every camera; return that user's ID.

Constraints:

  • 1 <= permissions.length <= 10^5
  • Each entry contains exactly three strings, each of length 1 to 50.
  • User IDs begin with user, group IDs begin with group, and camera IDs begin with camera.
  • The group-membership relationships are acyclic.
  • Exactly one admin user is guaranteed to exist.

Example 1:
Input: [["user1", "cameraOwner", "camera1"], ["user1", "groupMember", "group1"], ["group1", "cameraOwner", "camera2"], ["group1", "groupMember", "group2"], ["group2", "cameraOwner", "camera3"], ["user2", "cameraOwner", "camera3"], ["user2", "groupMember", "group1"], ["user3", "groupMember", "group2"], ["user3", "cameraOwner", "camera1"]]
Output: "user1"
Explanation: user1 has camera1 directly and inherits access to camera2 and camera3 through group1 and nested group2.

Example 2:
Input: [["user1", "cameraOwner", "camera1"], ["user1", "groupMember", "group1"], ["group1", "groupMember", "group2"], ["group2", "cameraOwner", "camera2"], ["group2", "groupMember", "group3"], ["group3", "cameraOwner", "camera3"], ["user2", "cameraOwner", "camera1"], ["user2", "cameraOwner", "camera2"]]
Output: "user1"

Example 3:
Input: [["user1", "groupMember", "group1"], ["user1", "groupMember", "group2"], ["group1", "cameraOwner", "camera1"], ["group2", "cameraOwner", "camera2"], ["group2", "cameraOwner", "camera3"], ["user2", "groupMember", "group1"], ["user2", "cameraOwner", "camera2"]]
Output: "user1"

Implement admin_user(permissions) and return the unique admin ID as a string.