Aggregate expenses by person, trip, and category
Company: Rippling
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to perform efficient data aggregation, use associative data structures (maps/dictionaries), and reason about time and space complexity when processing large collections of records.
Part 1: Aggregate Total Expenses Per Employee
Constraints
- 0 <= len(records) <= 10^6
- Each record has exactly 4 fields: (employee_id, trip_id, category, amount)
- employee_id, trip_id, and category are strings
- 0 <= amount <= 10^9
Examples
Input: ([('E1', 'T1', 'MEAL', 50), ('E2', 'T2', 'HOTEL', 120), ('E1', 'T3', 'TRANSPORT', 30), ('E2', 'T2', 'MEAL', 20)],)
Expected Output: {'E1': 80, 'E2': 140}
Explanation: E1 has 50 + 30 = 80. E2 has 120 + 20 = 140.
Input: ([],)
Expected Output: {}
Explanation: Edge case: no expense records means no totals.
Hints
- A hash map/dictionary is a natural way to accumulate totals by employee_id.
- You only need to scan the records once.
Part 2: Aggregate Expenses Per Trip for One Employee
Constraints
- 0 <= len(records) <= 10^6
- Each record has exactly 4 fields: (employee_id, trip_id, category, amount)
- employee_id, trip_id, and category are strings
- 0 <= amount <= 10^9
Examples
Input: ([('E1', 'T1', 'MEAL', 50), ('E1', 'T1', 'HOTEL', 70), ('E1', 'T2', 'TRANSPORT', 30), ('E2', 'T1', 'MEAL', 10)], 'E1')
Expected Output: {'T1': 120, 'T2': 30}
Explanation: For E1, trip T1 has 50 + 70 = 120, and T2 has 30.
Input: ([('E2', 'T1', 'MEAL', 10)], 'E1')
Expected Output: {}
Explanation: Edge case: the target employee has no matching records.
Hints
- Ignore records whose employee_id does not match the target.
- For matching records, accumulate amounts using trip_id as the dictionary key.
Part 3: Aggregate Expenses Per Category for One Employee
Constraints
- 0 <= len(records) <= 10^6
- Each record has exactly 4 fields: (employee_id, trip_id, category, amount)
- employee_id, trip_id, and category are strings
- 0 <= amount <= 10^9
Examples
Input: ([('E1', 'T1', 'MEAL', 50), ('E1', 'T2', 'MEAL', 20), ('E1', 'T2', 'HOTEL', 100), ('E2', 'T1', 'MEAL', 999), ('E1', 'T3', 'TRANSPORT', 30)], 'E1')
Expected Output: {'MEAL': 70, 'HOTEL': 100, 'TRANSPORT': 30}
Explanation: For E1, MEAL is 50 + 20, HOTEL is 100, and TRANSPORT is 30.
Input: ([('E2', 'T1', 'MEAL', 10)], 'E1')
Expected Output: {}
Explanation: Edge case: the target employee has no matching records.
Hints
- You do not need to group by trip here; only by category.
- A single pass with a dictionary is enough.