Fixed-Size Memory Pool
Updated
Problem statement
Implement a pool containing capacity equal-size blocks. Blocks are represented by integer handles 0 through capacity - 1; handle 0 is valid.
Process operations from first to last:
ALLOCremoves and returns the first free block. If no block is free, return-1and leave the pool unchanged.FREE handlereleases a currently allocated block and places it at the front of the free list. Every free is valid.
Initially, the free list is [0,1,...,capacity-1]. Released blocks are therefore reused in last-freed-first order, before any blocks still at the back of the free list. Return one integer for each ALLOC, in request order. FREE produces no output entry.
The handles stand for equal-size memory slots. You do not need real memory addresses or calls to a system memory allocator.
Function
processMemoryPool(capacity: int, operations: String[]) → int[]Examples
Example 1
capacity = 3operations = ["ALLOC","ALLOC","FREE 0","FREE 1","ALLOC","ALLOC","ALLOC","ALLOC"]return = [0,1,1,0,2,-1]The first allocations use 0 and 1. Freeing 0 then 1 puts 1 before 0 in the free list. The remaining untouched block 2 is used next; exhaustion returns -1.
Example 2
capacity = 0operations = ["ALLOC","ALLOC"]return = [-1,-1]A zero-capacity pool cannot allocate. Each failed allocation leaves the empty pool unchanged.
Example 3
capacity = 2operations = []return = []No allocation request occurs, so no output entries are returned.
Constraints
0 <= capacity <= 10^5.0 <= operations.length <= 10^5.- Each operation is exactly
ALLOCorFREE handle, where the handle is a decimal integer in[0,capacity). - Every
FREEnames a currently allocated block; double frees and foreign handles are excluded. - One pool is used. All blocks have the same size. Alignment, concurrency, multiple block sizes and real memory addresses are not part of this problem.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
- Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Linked List Add and Remove OperationsAdobe · PHONE SCREENAdobe interview questions
- Token RingBloomberg LP · PHONE SCREENBloomberg LP interview questions
- LRU CacheAthenahealth · PHONE SCREEN
- 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
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- LRU CacheGimlet Labs · PHONE SCREEN
- LRU CacheBNSF Railway · PHONE SCREENBNSF Railway interview questions
- Max Frequency TrackerProcol · PHONE SCREEN