← JavaScript Coding

Data Selection

hardarraysfilteringsorting
Asked at
Datadog
DoorDash
Instacart
Snowflake
Uber
Datadog, DoorDash, Instacart, Snowflake, Uber

Problem statement

Implement selectData(sessions, options), a utility for selecting gym session records from a data set.

[
  { user: 8, duration: 50, equipment: ['bench'] },
  { user: 7, duration: 150, equipment: ['dumbbell', 'kettlebell'] },
  { user: 1, duration: 10, equipment: ['barbell'] },
  { user: 7, duration: 100, equipment: ['bike', 'kettlebell'] },
  { user: 7, duration: 200, equipment: ['bike'] },
  { user: 2, duration: 200, equipment: ['treadmill'] },
  { user: 2, duration: 200, equipment: ['bike'] },
]

Session Fields

  • user: user id.
  • duration: duration in minutes.
  • equipment: equipment used in the session, sorted alphabetically.

Options

  • user: include only sessions for this user.
  • minDuration: include only sessions with duration greater than or equal to this value.
  • equipment: include only sessions containing every requested equipment item.
  • merge: when true, merge sessions by user into one row per user.

When merging, sum durations and combine equipment into a sorted unique list.

Requirements & constraints

  • →Do not mutate input sessions.
  • →Equipment in merged results should be unique and alphabetically sorted.
  • →When `merge` is true, return users sorted by user id.

How to approach Data Selection

The strategy an interviewer expects you to reach for.

Data selection is filter-then-optionally-merge — gym sessions filtered by user/duration/equipment, then optionally grouped. Naive move Mutate the input array in place or return the same object references to callers. Where it breaks Callers mutate returned sessions and corrupt the source. Filters must AND together. Merge mode needs aggregation (sum duration, unique equipment). The fix Filter with all provided options. If merge is false, return shallow clones. If true, group with a Map by user and union equipment via Set. Remember: Filter first; clone when not merging; Map+Set when merging. Say this in the interview: I'd apply every filter as an AND, return clones for the non-merge path, and aggregate with a Map when merge is true so callers cannot mutate my source data.

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 JS Coding questions

View all JS Coding →
JS CodingMedium

Data Merging (Gym Sessions)

JS CodingMedium

LRU Cache

JS CodingEasy

Two Sum

JS CodingEasy

Contains Duplicate

JS CodingEasy

Valid Anagram

JS CodingMedium

Group Anagrams

Explore related prep hubs

  • All JavaScript coding
  • JavaScript interview questions
  • JS polyfill interview questions
  • Debounce & throttle questions
  • React interview hub
  • Datadog interview
  • DoorDash interview
  • Instacart interview
  • Snowflake interview
  • Uber interview