← JavaScript Coding

Data Merging (Gym Sessions)

medium
Asked at
Stripe
Stripe

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.

A strong solution for Data Merging (Gym Sessions) starts with the invariant: what should always be true after each loop step or recursive call? Once that invariant is clear, the implementation is usually small. Initialize the state, process each input item, update the state, and return the final value. After coding, walk through one normal case and one edge case. This catches most bugs and gives the interviewer confidence that you understand the trade-offs.

Premium

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
Unlock the full solution →

Already a member? Log in to open the workspace.

More JavaScript Coding questions

View all JavaScript Coding →
JS CodingHard

Data Selection

JS CodingMedium

LRU Cache

JS CodingEasy

Two Sum

JS CodingEasy

Contains Duplicate