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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Add User With Capacity
00:00
5 left

Add User With Capacity

EasyPython

Problem

A Dropbox team maintains a shared storage limit. Implement ADD_USER(user_id, capacity) to register a user with a requested storage capacity.

The operation succeeds only when the user ID is not already registered, the capacity is positive, and the team's total assigned capacity does not exceed max_capacity. On success, add the user and return True. On failure, leave the user mapping unchanged and return False.

Formal Specification

Implement add_user(users, user_id, capacity, max_capacity).

  • users is a dictionary mapping user IDs to assigned capacities.
  • user_id is a string.
  • capacity and max_capacity are integers measured in gigabytes.
  • The function mutates users only after all validation checks pass.
  • Return a Boolean indicating whether the user was added.

The existing dictionary may contain valid users, and its assigned capacities count toward the team's limit.

Constraints

  • 0 <= len(users) <= 10^5
  • 1 <= capacity <= 10^9
  • 0 <= max_capacity <= 10^14
  • Every existing capacity is positive
  • User IDs are non-empty strings

Function Signature

def add_user(users, user_id, capacity, max_capacity):
Interviewer

Your question is Add User With Capacity. 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.