KatInterview

Expense Reimbursement Ledger Simulator

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designdesign-dsarrayshash-table

Last updated

Simulate an expense tracking ledger executing sequential API calls with validation and error checks.

Design an in-memory expense tracking module that processes a series of ledger commands.

Each operation is passed as a string command name with a corresponding list of arguments. You will execute the operations sequentially and return a list containing the output of each operation.

Data Model

An expense dictionary contains the following fields:

  • id (int): Unique identifier assigned sequentially starting at 1.
  • amount (float or int): Numerical value of the expense.
  • category (str): Category label.
  • description (str): Brief description.
  • is_reimbursed (bool): Reimbursement status (defaults to False).

Operations & Rules

  1. "add_expense": Takes a payload dictionary [data].

    • Rule: If data["amount"] <= 0, do not create an expense. Append {"error": "amount must be greater than zero"} to the result.
    • If valid, assign a unique id starting from 1 (incrementing by 1 for each successful addition). Set is_reimbursed to data.get("is_reimbursed", False).
    • Store a copy of the new record and append a copy of it to the result.
  2. "delete_expense": Takes an [expense_id].

    • If expense_id is not present in the system, append {"error": "expense not found"}.
    • Otherwise, remove the record from the system and append None to the result.
  3. "list_expenses": Takes an empty argument list [].

    • Returns a list of all active expenses ordered by their id in ascending order. Append this list to the result.
  4. "toggle_reimbursement": Takes an [expense_id].

    • If expense_id is not present in the system, append {"error": "expense not found"}.
    • Rule: If the expense exists but has amount > 1000, do not toggle its status. Append {"error": "approval required"}.
    • If valid, flip the boolean is_reimbursed value for that expense and append a copy of the updated expense to the result.
  5. Any unrecognized operation name: Append {"error": "unknown operation"}.

Constraints

  • $1 \le \text{len(operations)} \le 1000$
  • operations[i] matches one of the operation strings described above.
  • arguments[i] contains the matching arguments for operations[i].
  • Expense amounts are finite numbers.

Edge Case Notes

  • Rejected add_expense calls do not increment the ID counter.
  • IDs are persistent and are never reused, even after deletion.

Examples

Example 1

Input: manage_expenses(['add_expense', 'toggle_reimbursement', 'list_expenses'], [[{'amount': 50, 'category': 'Travel', 'description': 'Uber'}], [1], []])

Output: [{'id': 1, 'amount': 50, 'category': 'Travel', 'is_reimbursed': False, 'description': 'Uber'}, {'id': 1, 'amount': 50, 'category': 'Travel', 'is_reimbursed': True, 'description': 'Uber'}, [{'id': 1, 'amount': 50, 'category': 'Travel', 'is_reimbursed': True, 'description': 'Uber'}]]

Example 2

Input: manage_expenses(['add_expense', 'add_expense', 'toggle_reimbursement', 'delete_expense', 'list_expenses'], [[{'amount': 1250, 'category': 'Equipment', 'description': 'Monitor'}], [{'amount': -5, 'category': 'Meals', 'description': 'Lunch'}], [1], [9], []])

Output: [{'id': 1, 'amount': 1250, 'category': 'Equipment', 'is_reimbursed': False, 'description': 'Monitor'}, {'error': 'amount must be greater than zero'}, {'error': 'approval required'}, {'error': 'expense not found'}, [{'id': 1, 'amount': 1250, 'category': 'Equipment', 'is_reimbursed': False, 'description': 'Monitor'}]]

Hints

Hint 1

Use a hash map (dictionary) keyed by `id` to store active expenses for O(1) retrieval, deletion, and updates.

Hint 2

Maintain a separate integer variable for `next_id` starting at 1 that increments only upon successful creation of an expense.

Hint 3

Always return copies of stored dictionaries when appending to the output list to prevent unwanted mutation side effects.