Interview Verified
Roblox SWE Intern - Aligned Memory Allocator
AskedNov 2025
SolvedSolved
LanguageLua
PostedJul 21, 2026
This is word for word the details they lay out for you to answer this problem.
You are given an array memory consisting only of 0s and 1s.
memory[i] = 0 means the memory unit is free.
memory[i] = 1 means the memory unit is occupied.
Memory is divided into segments of 8 units, meaning every allocated block must begin at an index divisible by 8 (0, 8, 16, ...). A block may span multiple segments.
You must process two types of queries.
Query Type 1 — Allocate
[0, x]
Allocate a contiguous block of exactly x free memory units.
Rules:
The starting index of the block must be divisible by 8.
Choose the leftmost valid block.
If multiple segments are required, all x units must be free.
Every successful allocation receives a unique integer ID.
Allocation IDs begin at 1 and increase by one after every successful allocation.
Store this ID for every allocated memory unit.
Return:
the starting index of the allocated block (0-based), or
-1 if no valid aligned block exists.
Query Type 2 — Erase
[1, id]
If a block with allocation ID id exists:
Free the entire block.
Return the size of the deleted block.
Otherwise return -1.
Return an array containing the answer for every query.
Constraints
8 ≤ memory.length ≤ 320
memory.length is divisible by 8
Number of queries ≤ 10^5
Example
memory =
[0,1,0,0,0,1,1,0,
0,0,0,0,1,1,1,1]
queries =
[
[0,2],
[0,1],
[0,1],
[1,1],
[0,3],
[1,4],
[0,4]
]
Output:
[8,0,-1,2,8,-1,-1]