Aligned Free-List Memory Allocator
Updated
Problem statement
Implement an allocator for one fixed-size memory region. The region contains totalBytes bytes, begins at address 0, and is divided into blocks of alignment bytes.
You are given equal-length arrays operations and values. Process them in order:
allocaterequestsvalues[i]bytes. Round the request up to a whole number of alignment blocks, then reserve the first free contiguous range large enough to hold it. Return that range's starting address, or-1when no free range is large enough.freereleases the live allocation whose starting address isvalues[i]. Every free operation is guaranteed to reference a currently live allocation.
Return the addresses produced by allocate operations, in encounter order. Maintain free memory as an address-ordered linked list of maximal free ranges. Split a range when allocating and coalesce adjacent ranges when freeing.
Function
runAllocator(totalBytes: int, alignment: int, operations: String[], values: int[]) → int[]Examples
Example 1
totalBytes = 32alignment = 8operations = ["allocate","allocate","free","allocate"]values = [9,8,0,16]return = [0,16,0]The 9-byte request rounds up to 16 bytes and starts at 0. The 8-byte request starts at 16. Freeing address 0 makes the first 16 bytes available again, so the last request returns 0.
Example 2
totalBytes = 24alignment = 8operations = ["allocate","allocate","allocate","free","allocate"]values = [8,16,8,0,8]return = [0,8,-1,0]The first two allocations fill the region. The third request fails. After address 0 is freed, one block is available there and the final request succeeds.
Constraints
1 <= alignment <= 1024, andalignmentis a power of two.alignment <= totalBytes <= 1000000.totalBytesis divisible byalignment.0 <= operations.length <= 200000.operations.length == values.length.- Every operation is
allocateorfree. - For
allocate,1 <= values[i] <= totalBytes. - For
free,values[i]is the starting address of a live allocation.
More Hudson River Trading interview questions
- Tiered Expiring Item StorePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Read a File from Distributed ChunksONSITE INTERVIEW · Seen Aug 2026
- Best Expected Result in Simplified BlackjackONSITE INTERVIEW · Seen Apr 2025
- Format a Signed Integer with CommasPHONE SCREEN · Seen Nov 2026
- Order and Batch Open-Close LifecyclePHONE SCREEN · Seen Nov 2026
- Count Palindrome Addition ChoicesOA · Seen Nov 2026
- Tilting Maze BallPHONE SCREEN · Seen Oct 2026
- Count Multiples of Three with Two SevensOA · Seen Oct 2026
- Recursively Expanded Shell Command CountsOA · Seen Oct 2026
- Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
Similar questions at other companies
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- LRU Cache with Hit and Miss CountersLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Least Recently Used CacheGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- LRU CacheIntuit · ONSITE INTERVIEWIntuit interview questions
- LRU CacheOracle · ONSITE INTERVIEWOracle interview questions