Memory Allocator
Updated
Problem statement
You are given a continuous block of n bytes, addressed from 0 to n - 1. Initially, every byte is free.
Implement simulateMemoryAllocator, which processes a list of allocator operations and returns one output string for each operation.
The allocation strategy is fixed for the whole run:
first_fit: choose the lowest-address free block whose length is at least the requested size.best_fit: choose the smallest free block whose length is at least the requested size. If multiple blocks have the same length, choose the lowest starting address.
The operation list contains only these commands:
malloc k: allocate a contiguous free block of exactlykbytes using the selected strategy. Return the starting address as a string, or"-1"if no eligible block exists.free p: free the allocated block that starts at addressp. Adjacent free blocks must be merged. Return"OK"if a block was freed, or"INVALID"ifpis not the start of a currently allocated block.
Allocated blocks cannot be moved or compacted. If an allocation uses only part of a free block, the remaining bytes stay free.
Return an array containing the output for each operation in order.
Follow-up focus
The main allocator behavior is first fit. Best fit uses the same malloc and free operations as a strategy variant. In both cases, the key implementation detail is keeping free intervals consistent and merging neighboring intervals after free.
Overview
This task implements memory allocation over a fixed contiguous memory region, including malloc, free, merging adjacent free intervals, and a best-fit follow-up.
Function
simulateMemoryAllocator(n: int, strategy: String, operations: String[]) → String[]Examples
Example 1
n = 8strategy = "first_fit"operations = ["malloc 4", "malloc 4", "free 4", "malloc 3", "free 1", "free 0", "malloc 5"]return = ["0", "4", "OK", "4", "INVALID", "OK", "-1"]After freeing the block at 4, first fit reuses that lowest eligible address for malloc 3. free 1 is invalid because 1 is not the start of an allocated block. The final request fails because no contiguous free block has size 5.
Example 2
n = 12strategy = "best_fit"operations = ["malloc 5", "malloc 3", "malloc 2", "free 0", "free 8", "malloc 4", "free 5", "malloc 6"]return = ["0", "5", "8", "OK", "OK", "8", "OK", "0"]After freeing starts 0 and 8, the free blocks are [0, 4] and [8, 11]. Best fit chooses the smaller eligible block at 8 for malloc 4.
Constraints
1 <= n <= 10^9strategyis either"first_fit"or"best_fit".1 <= operations.length <= 10^4- Each operation is either
"malloc k"or"free p". - For
mallocoperations,1 <= k <= n. - For
freeoperations,pis an integer address. It may or may not be the start of an allocated block.
More OpenAI Phone Screen questions
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Prefix Matrix Products and AutogradPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Streaming Entropy, Part 1: Batch EntropyPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Streaming Entropy, Part 2: Numerically Stable EntropyPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Streaming Entropy, Part 3: Block-wise EntropyPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Streaming Entropy, Part 4: Stable Streaming EntropyPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Delivery Cost TrackerRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- In-Memory Meeting Room ManagerRunloop · PHONE SCREEN
- Range ModuleWalmart · PHONE SCREENWalmart interview questions
- Calendar Event System with Pagination and IntersectionLead Bank · ONSITE INTERVIEWLead Bank interview questions
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayAirbnb · OAAirbnb interview questions
- Worker Management with Promotions and Double-Paid TimeInstacart · ONSITE INTERVIEWInstacart interview questions
- Worker Management, Part 4: Double-Paid IntervalsAnthropic · OAAnthropic interview questions
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Recipe Rating ServiceNubank · PHONE SCREEN
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions