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
Memory Allocation and Freeing
00:00
5 left

Memory Allocation and Freeing

MediumPython

Problem

Implement a simplified memory allocator for a Facebook News Feed service. The allocator manages a contiguous memory array and processes allocation and release commands in order.

Memory is represented by a list of integers where 0 means free and 1 means occupied. An allocation request must receive the first contiguous run of free cells large enough to satisfy the request. Mark the selected cells as occupied and return the starting index. If no suitable run exists, return -1. A release command provides the starting index of a previously successful allocation; free exactly that block.

Implement memory_manager(memory, commands). The function must return one result for every command: an integer starting index for an allocation, or -1 if allocation fails; for a release command, return True after freeing the block.

Each successful allocation is identified by its starting index. The input guarantees that release commands reference currently active allocations and that allocation sizes are positive.

Constraints

  • 1 <= len(memory) <= 10^5
  • Each memory value is either 0 or 1
  • 1 <= len(commands) <= 10^4
  • Allocation sizes are positive integers no greater than len(memory)
  • Release commands reference successful, currently active allocations

Function Signature

def memory_manager(memory, commands):
Interviewer

Your question is Memory Allocation and Freeing. 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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.