KatInterview

Implement a Timed Workspace Credit System

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designheaps-sortingdesign-dsarrays

Last updated

Process a sequence of timestamped operations managing workspace balances, immediate transfers, scheduled/cancelled future transfers, and ranked queries.

Design an in-memory credit tracking system that processes a series of timestamped commands. The system manages workspace accounts, supports instant and scheduled transfers, allows cancellation of scheduled transfers, and can query the top workspaces by balance.

Every command in operations has a corresponding argument list in arguments. The first entry of every argument list is an integer timestamp. Timestamps across calls are non-decreasing.

Before executing any operation at a given timestamp, all pending scheduled transfers due at or before timestamp (execute_at <= timestamp) must take effect in the chronological order of their scheduled execution time. If multiple transfers are scheduled for the exact same execute_at time, process them in the order they were scheduled (earlier schedule operations run first).


Command Specifications

  1. "create": [timestamp, workspace_id]

    • Creates a workspace with initial balance 0.
    • Returns True if created successfully.
    • Returns False if workspace_id already exists.
  2. "add": [timestamp, workspace_id, amount]

    • Increases the balance of workspace_id by amount.
    • Returns True if successful.
    • Returns False if workspace_id does not exist or amount <= 0.
  3. "transfer": [timestamp, source, destination, amount]

    • Instantly transfers amount credits from source to destination.
    • Returns True if successful.
    • Returns False if:
      • source or destination does not exist,
      • source == destination,
      • amount <= 0, or
      • source has strictly less than amount credit.
  4. "top_k": [timestamp, k]

    • Returns a list of up to k workspace IDs sorted descending by current balance.
    • Tie-breaking: If two workspaces have equal balances, order them lexicographically ascending by workspace_id.
    • If k <= 0, return []. If k exceeds the total number of existing workspaces, return all workspaces in sorted order.
  5. "schedule": [timestamp, source, destination, amount, execute_at]

    • Schedules a future transfer of amount from source to destination to execute at execute_at.
    • Returns an integer transfer_id (1-indexed counter starting from 1 for the first valid scheduled transfer) if successfully scheduled.
    • Returns None if:
      • source or destination does not exist,
      • source == destination,
      • amount <= 0, or
      • execute_at <= timestamp.
    • Note: Credit sufficiency is not checked when scheduling; it is evaluated when the transfer comes due.
  6. "cancel": [timestamp, workspace_id, transfer_id]

    • Cancels a pending scheduled transfer identified by transfer_id.
    • workspace_id must match the source workspace of the scheduled transfer.
    • Returns True if successfully cancelled.
    • Returns False if transfer_id does not exist, has already been processed or cancelled, or if workspace_id is not the source workspace.

Processing Due Scheduled Transfers

When a scheduled transfer becomes due (execute_at <= current_timestamp):

  • If it has been cancelled or previously executed, ignore it.
  • If source or destination no longer exists, source == destination, amount <= 0, or source currently has fewer than amount credits, the transfer fails silently (balances remain unchanged).
  • Otherwise, deduct amount from source and add amount to destination.

Constraints

  • 1 <= len(operations) == len(arguments) <= 10^4
  • timestamp values are non-decreasing across consecutive operations.
  • Workspace IDs are strings.
  • Credit amounts and k are integers.
  • execute_at is an integer timestamp.

Examples

Example 1

Input: workspace_credit_system(['create', 'add', 'add', 'transfer', 'top_k'], [[1, 'a'], [2, 'a', 100], [3, 'b', 50], [4, 'a', 'b', 30], [5, 2]])

Output: [True, True, False, False, ['a']]

Example 2

Input: workspace_credit_system(['create', 'create', 'add', 'schedule', 'top_k', 'top_k'], [[1, 'a'], [2, 'b'], [3, 'a', 100], [4, 'a', 'b', 40, 10], [5, 2], [10, 2]])

Output: [True, True, True, 1, ['a', 'b'], ['a', 'b']]

Example 3

Input: workspace_credit_system(['create', 'create', 'add', 'schedule', 'cancel', 'top_k'], [[1, 'a'], [2, 'b'], [3, 'a', 100], [4, 'a', 'b', 20, 20], [5, 'a', 1], [20, 2]])

Output: [True, True, True, 1, True, ['a', 'b']]

Hints

Hint 1

Before processing any command at the current timestamp, drain all pending transfers from a min-heap where execute_at <= current_timestamp.

Hint 2

Use a unique sequential counter to break ties in the min-heap for scheduled transfers with identical execute_at timestamps.

Hint 3

Maintain a hash map of active pending transfers so that cancellations can mark transfers as invalid in O(1) time.