KatInterview

Ranked Staff Roster Across Multiple Forums with Swap-Down

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designdesignhash-mapsimulation

Last updated

Simulate a batch of operations on per-forum ordered staff lists, enforcing seniority rules for adding, moving down one place, and removing members.

Ranked Staff Rosters

A platform hosts many forums. Each forum keeps its own roster of staff members, ordered by seniority. The member who joined the roster earliest is the most senior (position 0), and each later joiner sits below all earlier ones. A staff member may act on another member only if they are strictly more senior (a smaller position) than that member.

You are given a list of operations and a parallel list of argument lists. Replay the operations in order on an initially empty system and return a list containing the result of every operation, in the same order.

Operations

  • add_moderator(community_id, user_id) returns a boolean. Appends user_id to the bottom of the roster of community_id (the least senior position). If the user is already on that roster, change nothing and return False. Otherwise return True.

  • demote_moderator(community_id, actor_id, target_id) returns a boolean. Succeeds only if all of the following hold:

    1. Both actor_id and target_id are on the roster of community_id.
    2. actor_id is strictly more senior than target_id.
    3. target_id is not already in the last (least senior) position.

    On success, target_id swaps places with the member directly beneath them, and the result is True. Otherwise nothing changes and the result is False.

  • remove_moderator(community_id, actor_id, target_id) returns a boolean. Succeeds only if both people are on the roster of community_id and actor_id is strictly more senior than target_id. On success, target_id is removed (everyone below moves up one place) and the result is True. Otherwise nothing changes and the result is False.

  • get_moderators(community_id) returns a list of strings. The roster of community_id from most senior to least senior. If the forum is unknown or its roster is empty, return [].

Notes

  • Forums are completely independent; the same user can be on several rosters with unrelated positions.
  • A member cannot act on themselves, since nobody is strictly more senior than themselves.
  • A removed user may be added again later, and then joins at the bottom.
  • Failed operations never modify any state.
  • The returned list holds True/False for the three mutating operations and a list of strings for get_moderators.

Constraints

  • All ids are non-empty alphanumeric strings.
  • operations and args have the same length, at most 50,000.
  • Each operation name is one of the four listed above, and args[i] contains the matching arguments in the order given.
  • No roster ever holds more than 1,000 members.

Examples

Example 1

Input: run_staff_roster(['add_moderator', 'add_moderator', 'add_moderator', 'get_moderators', 'demote_moderator', 'get_moderators', 'demote_moderator', 'remove_moderator', 'remove_moderator', 'get_moderators'], [['programming', 'alice'], ['programming', 'bob'], ['programming', 'charlie'], ['programming'], ['programming', 'alice', 'bob'], ['programming'], ['programming', 'alice', 'bob'], ['programming', 'bob', 'charlie'], ['programming', 'alice', 'charlie'], ['programming']])

Output: [True, True, True, ['alice', 'bob', 'charlie'], True, ['alice', 'charlie', 'bob'], False, False, True, ['alice', 'bob']]

Example 2

Input: run_staff_roster(['add_moderator', 'add_moderator', 'add_moderator', 'demote_moderator', 'get_moderators', 'remove_moderator', 'get_moderators'], [['x', 'u1'], ['x', 'u2'], ['y', 'u1'], ['x', 'u1', 'u2'], ['x'], ['x', 'u2', 'u1'], ['x']])

Output: [True, True, True, False, ['u1', 'u2'], False, ['u1', 'u2']]

Hints

Hint 1

Keep, for each forum, an ordered list of users plus a dictionary from user to current index, so membership and seniority comparisons are O(1).

Hint 2

Check every precondition before touching any data, so a failed demote or remove leaves the state exactly as it was.

Hint 3

After deleting a member from the middle of the list, every later member shifts up by one, so refresh their stored indices. Also return a copy of the list from get_moderators, not the internal one.