Leftmost Memory Block Allocator (for mle also :)
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
TikTok reported this one in September 2026, and the title even jokes about MLE, which tells you what the grader punishes. You get a 0/1 memory array and a list of allocate and erase queries. Allocate finds the leftmost run of x free cells, marks it, and hands out IDs starting at 1. Erase frees a block by ID and returns its length. It's a simulation problem with a data structure twist. If the constraints are big and you blank on the efficient version, StealthCoder is the invisible safety net running during the live OA.
The problem
You are given an integer array memory containing only 0s and 1s. A value of 0 means that the memory unit is free, while 1 means that it is occupied. Process the two-element arrays in queries in order. Each query has one of two forms: [0, x] is an allocation query. Find the smallest index that begins a contiguous block of x free units. If a block exists, mark all of its units as occupied, assign the next allocation ID to the block, and return its starting index. Allocation IDs start at 1 and increase only after successful allocations. If no block fits, return -1. [1, id] is an erase query. If an active allocation has ID id, free every unit in that block and return its length. If the ID does not exist or has already been erased, return -1. Memory units that are occupied initially are not associated with an allocation ID and cannot be freed by an erase query. Return an integer array containing one result for every query. Function processMemoryQueries(memory: int[], queries: int[][]) → int[] Examples Example 1 memory = [0,0,1,0,0,0] queries = [[0,2],[0,3],[1,1],[0,3]] return = [0,3,2,-1] The first allocation takes indices 0 and 1 with ID 1. The second takes indices 3 through 5 with ID 2. Erasing ID 1 frees two units. No three-unit free block remains for the final query. Example 2 memory = [0,0,0,0] queries = [[1,1],[0,2],[1,1],[1,1]] return = [-1,0,2,-1] The first erase fails because no allocation exists. The allocation then creates ID 1 at index 0. Its first erase frees two units, while the repeated erase returns -1. Example 3 memory = [1,0,0,0,1,0,0] queries = [[0,3],[0,2],[1,1],[0,2]] return = [1,5,3,1] The first two allocations use starts 1 and 5. Erasing ID 1 frees its three-unit block, so the final allocation returns the newly available leftmost start 1. Constraints 1 <= memory.length Every value of memory is 0 or 1. 1 <= queries.length Every query contains exactly two integers. queries[i][0] is 0 or 1. 1 <= queries[i][1]
Reported by candidates. Source: FastPrep
Pattern and pitfall
The brute force is easy: scan the array for a run of x zeros on every allocation. That's O(n) per query, and the title's MLE hint says watch memory too. Keep a map from ID to (start, length) so erase is O(length) and returns -1 for unknown or already-erased IDs. Delete the entry on erase so repeat erases fail. The pitfalls: IDs only increment on successful allocations, and initially occupied cells have no ID, so they can never be freed. For speed, a segment tree storing prefix free run, suffix free run and best run per node lets you find the leftmost run of length x in O(log n), with range assign for allocate and erase. Don't store extra copies of memory per query. If the segment tree won't come to you under the clock, StealthCoder can hand you the structure during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Leftmost Memory Block Allocator (for mle also :) cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as design memory allocator. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass TikTok's OA.
TikTok reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Leftmost Memory Block Allocator (for mle also :) FAQ
What's the trick in the TikTok memory allocator problem?+
Track allocations in a hash map from ID to start and length. For finding the leftmost free block, a linear scan works if constraints are small. If they're large, use a segment tree with prefix, suffix and max free run per node.
Do allocation IDs increase on failed allocations?+
No. The ID counter only goes up after a successful allocation. A failed allocation returns -1 and leaves the counter alone. Example 1 shows this: IDs 1 and 2 are given out, and the failed final query doesn't consume one.
Can I erase the initially occupied cells?+
No. Cells that are 1 at the start have no ID, so no erase query can free them. Only blocks created by your own successful allocations go into the ID map. Any other ID returns -1.
How do I handle repeated erases of the same ID?+
Remove the ID from the map on the first successful erase. The second erase then finds nothing and returns -1. Example 2 tests exactly this, so check it before submitting.
How do I prepare for this in 48 hours?+
Write the simple scan version first and pass all three examples. Then practice a segment tree with range assign and a leftmost-fit query. Test edge cases like x larger than the array, erasing before any allocation, and freeing then reallocating the same spot.