KatInterview

Compute Spending Totals by Worker, Journey, and Type

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashinghash-mapsaggregationgrouping

Last updated

Tests efficient multi-key aggregation using hash maps to group and sum records in a single pass over large datasets.

You receive a collection of spending records. Each record is a dictionary containing:

  • employee_id (string): identifier for the worker who incurred the expense
  • trip_id (string): identifier for the journey during which the expense occurred
  • category (string): type of expense (examples: "MEAL", "HOTEL", "TRANSPORT")
  • amount (integer): cost in cents or smallest currency unit

Write a function that computes three separate aggregations and returns them in a single dictionary:

  1. Global worker totals: sum of all expenses for every worker in the dataset
  2. Journey breakdown: sum of expenses per journey, limited to one specified worker
  3. Category breakdown: sum of expenses per category, limited to the same specified worker

The function accepts a list of records and one employee_id string. It must return a dictionary with exactly three keys:

  • "employee_totals": maps every employee_id in the input to their total spending (integer)
  • "trip_totals": maps each trip_id belonging to the specified employee to the total spent on that journey
  • "category_totals": maps each category used by the specified employee to the total spent in that category

If the specified employee_id does not appear in the records, "trip_totals" and "category_totals" must be empty dictionaries (not None).

Constraints

  • 0 ≤ number of records ≤ 1,000,000
  • All string fields are non-empty
  • Amounts may be zero or negative (refunds)
  • Keys are case-sensitive: "MEAL" and "meal" are distinct categories
  • Your solution should process the records efficiently; avoid repeated full scans

Examples

Example 1

Input: compute_spending_totals([{'employee_id': 'e1', 'trip_id': 't1', 'category': 'MEAL', 'amount': 25}, {'employee_id': 'e1', 'trip_id': 't1', 'category': 'HOTEL', 'amount': 120}, {'employee_id': 'e2', 'trip_id': 't2', 'category': 'TRANSPORT', 'amount': 40}, {'employee_id': 'e1', 'trip_id': 't2', 'category': 'MEAL', 'amount': 15}], 'e1')

Output: {'employee_totals': {'e1': 160, 'e2': 40}, 'trip_totals': {'t1': 145, 't2': 15}, 'category_totals': {'MEAL': 40, 'HOTEL': 120}}

Example 2

Input: compute_spending_totals([{'employee_id': 'alice', 'trip_id': 'trip-1', 'category': 'MEAL', 'amount': 10}, {'employee_id': 'alice', 'trip_id': 'trip-1', 'category': 'MEAL', 'amount': 20}, {'employee_id': 'bob', 'trip_id': 'trip-1', 'category': 'MEAL', 'amount': 99}], 'alice')

Output: {'employee_totals': {'alice': 30, 'bob': 99}, 'trip_totals': {'trip-1': 30}, 'category_totals': {'MEAL': 30}}

Example 3

Input: compute_spending_totals([], 'missing')

Output: {'employee_totals': {}, 'trip_totals': {}, 'category_totals': {}}

Hints

Hint 1

All three aggregations are sum-by-key operations over the same input; consider computing them together in a single traversal.

Hint 2

Use hash maps with .get(key, 0) to accumulate totals without checking existence first.

Hint 3

Only two of the three outputs are filtered by employee; the global employee totals include everyone.