← Problems61. Apply entitlement events to seat stateMediumPython
00:00 / 30:00

Apply entitlement events to seat state

MediumNot attempted yetAsked at Adobe

Seat assignments are stored as an event log rather than a table, and the log is replayed to rebuild who currently holds a seat. The events reach you unsorted because they are read from several partitions at once; seq is the true order.

Input schema

solve(events: list[dict]) -> list[dict]

Each event has seq (int), org_id (int), user_id (str) and action, which is either ASSIGN or REVOKE.

Return one dict per organisation, with keys in this order:

org_id, seats, users

Constraints

  • Apply events in ascending seq; seq is unique
  • Assigning a seat the user already holds changes nothing — it is not a second seat
  • Revoking a seat the user does not hold changes nothing, and is not an error
  • users is the list of holders, sorted ascending; seats is how many there are
  • An organisation whose seats all end up revoked is left out entirely
  • Order by org_id
  • Up to 5,000,000 events

Example 1two orgs, one revoke

Input · events
seqactionorg_iduser_id
1ASSIGN1ana
2ASSIGN1bo
3ASSIGN2cy
4REVOKE1ana
Output
org_idseatsusers
11["bo"]
21["cy"]

Example 2events arrive out of order

Input · events
seqactionorg_iduser_id
3ASSIGN5dee
1ASSIGN5dee
2REVOKE5dee
Output
org_idseatsusers
51["dee"]

Topics

data structuresevent sourcing

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.