Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Balanced Subset Sampling
00:00
5 left

Balanced Subset Sampling

HardPython

Problem

Cresta model-training workflows may need a fixed number of examples across intent classes while respecting the available examples in each class. Given a requested total n and the capacity of every class, return a deterministic allocation that is as balanced as possible.

Use the following definition of balanced: maximize the smallest allocation across all classes, then distribute any remaining examples so that no eligible class receives more than one extra example over another. Classes are processed in their original order when a tie remains.

Formal Specification

Implement balance_samples(n, capacities), where n is a nonnegative integer and capacities[i] is the number of available examples for class i. Return an array allocation of the same length such that 0 <= allocation[i] <= capacities[i] and sum(allocation) == n.

It is guaranteed that n does not exceed the total capacity. Do not sample actual examples, only compute counts.

Constraints

  • 0 <= n <= 10^18
  • 1 <= len(capacities) <= 2 * 10^5
  • 0 <= capacities[i] <= 10^18
  • n <= sum(capacities)
  • The output length must equal len(capacities)

Function Signature

def balance_samples(n, capacities):
Interviewer

Your question is Balanced Subset Sampling. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.