Problem · Array
Memory Allocator
Learn this problemProblem statement
Given a memory array consisting of 0s and 1s (0 = free, 1 = occupied). Memory is aligned by 8.
Support the following two operations, provided as a list of strings:
alloc x— Find the leftmost contiguous block ofxfree units whose starting index is divisible by 8. Allocate it (mark as occupied and assign a unique ID). Return the starting index, or-1if not found.erase id— Scan the entire array, reset all positions with the given ID to 0, and return the number of cleared units.
IDs are assigned incrementally starting from 1. Given the initial memory array and a list of operations, return an array of integer results — one per operation.
Note: The starting index of an allocated block must be divisible by 8. The block itself may span beyond the next multiple of 8.
Function
memoryAllocator(memory: int[], operations: String[]) → int[]Examples
Example 1
memory = [0,0,0,0,0,0,0,0]operations = ["alloc 3","erase 1"]return = [0, 3]alloc 3: starting index 0 is divisible by 8, positions 0–2 are all free → allocate with ID=1, return 0. erase 1: positions 0–2 have ID=1 → clear them and return 3.
Example 2
memory = [1,1,1,1,1,1,1,1]operations = ["alloc 1"]return = [-1]alloc 1: the only valid starting index is 0, but position 0 is occupied → return -1.
More Tiktok problems
- Can Reach the Exit with TeleportsOA · Seen Jul 2026
- Check Monotonic TriplesOA · Seen Jul 2026
- Shift Every K-th ConsonantOA · Seen Jul 2026
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Sort Matrix BordersOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Validate 3x3 Digit WindowsOA · Seen Jul 2026