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.
Examples
01 · 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.
02 · 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
- LRU Cache with TTL ExpirationONSITE INTERVIEW · Seen May 2026
- Maximum Candies with At Most Two Types in a LineONSITE INTERVIEW · Seen May 2026
- Top-K KOLs by Total LikesONSITE INTERVIEW · Seen May 2026
- Compute Walking DistanceSeen Mar 2026
- Count Sawtooth SubarraysSeen Mar 2026
- Format TextSeen Mar 2026
- TikTok Shopping SpreeSeen Mar 2025
- TikTok Video Relevance AdjustmentSeen Mar 2025
public int[] memoryAllocator(int[] memory, String[] operations) {
// write your code here
}
memory[0,0,0,0,0,0,0,0]
operations["alloc 3","erase 1"]
expected[0, 3]
sign in to submit