Data Merging (Gym Sessions)
Problem statement
Implement a method mergeData(sessions) that returns a unified view of each user's activities by merging gym session data.
Merging Rules
- Duration: Sum up the duration fields for the same user.
- Equipment: Combine all equipment used, de-duplicate them, and sort alphabetically.
- Order: The output order must match the earliest occurrence of each user in the input.
Example
const sessions = [
{ user: 1, duration: 10, equipment: ['bench'] },
{ user: 1, duration: 20, equipment: ['bike'] }
];
mergeData(sessions); // [{ user: 1, duration: 30, equipment: ['bench', 'bike'] }]
How to approach Data Merging (Gym Sessions)
The strategy an interviewer expects you to reach for.
Gym-session merge: group rows by user and combine durations/equipment without losing data or double-counting. Naive move Nested loops comparing every session to every other session. Quadratic and mutation-prone. Where it breaks Duplicate users appear twice. Equipment lists get duplicate strings. Original objects get mutated if you push references. The fix Use a Map keyed by user. On each session, create or update an aggregate: add duration, union equipment via Set. Emit the map values at the end. Remember: Group with Map; union equipment with Set; don't mutate input sessions. Say this in the interview: I'd aggregate sessions in a Map by user, sum durations, and store equipment in a Set so merging stays linear and duplicate-free.
The full solution is part of HelloFrontend Pro
The question above is free to read in full. Upgrade to unlock the interactive workspace and the senior-level walkthrough that go with it.
- Runnable editor with the hidden test suite
- Progressive hints that unlock as you get stuck
- Senior-level reference solution with a line-by-line walkthrough
Already a member? Log in to open the workspace.