Leftmost Memory Block Allocator (for mle also :)
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ByteDance OA reported in August 2026 looks like a memory allocator toy, and the trap is hiding in plain sight. You get a 0/1 array and a list of allocate and erase queries. Each allocation needs the leftmost run of x free cells, and each erase frees a block by ID. The constraint says O(n^2 * q) passes, so brute force is allowed. Most people still lose points on the edge cases: IDs that only increment on success, initially occupied cells that can never be erased, and double erases. If you blank mid-assessment, StealthCoder runs invisibly as a safety net.
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. A solution with time complexity no worse than O(memory.length^2 * queries.length) will fit within the execution time limit. Function processMemoryQueries(memory: int[], queries: int[][]) → int[] Examples Example 1 memory = [0,1,0,0,0,1,1,0,0,0,1,0,0] queries = [[0,2],[0,1],[0,1],[1,2],[1,4],[0,4]] return = [2,0,4,1,-1,-1] The first three allocations start at indices 2, 0, and 4, receiving IDs 1, 2, and 3. Erasing ID 2 frees one unit. ID 4 does not exist, and the final allocation cannot find four consecutive free units. Example 2 memory = [1,0,0] queries = [[1,1],[0,2],[1,1],[0,2]] return = [-1,1,2,1] The initially occupied unit has no allocation ID, so the first erase fails. Allocating two units starts at index 1 with ID 1. Erasing that ID frees two units, and the final allocation uses the same leftmost block. 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
This is a simulation problem. Scan the array left to right, track the current run of zeros, and when the run hits x, the start index is i - x + 1. Mark those cells as 1, store id -> (start, length) in a hash map, then bump the counter. Erase looks up the id, sets the cells back to 0, deletes the entry, and returns the length. A missing id returns -1, which also covers already erased ones. The pitfalls are incrementing the ID on a failed allocation, forgetting that original 1s have no ID, and resetting the run counter wrongly after hitting a 1. Also check x larger than memory.length, which should just return -1. The generous complexity bound means don't over-engineer with segment trees. If the logic slips under pressure, StealthCoder can hand you a clean pass as a hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 ByteDance's OA.
ByteDance reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Leftmost Memory Block Allocator (for mle also :) FAQ
How hard is the ByteDance memory allocator question really?+
Easy to medium. The algorithm is a linear scan for a run of zeros plus a hash map. The difficulty is carefulness, not insight. Most failures come from ID handling and run-reset bugs, not from complexity.
What's the trick to finding the leftmost free block?+
Walk the array keeping a counter of consecutive zeros. Reset it to 0 on every 1. The first time the counter equals x, the block starts at i - x + 1. That gives the smallest start index automatically.
Do I need a fancy data structure here?+
No. The stated bound of O(n^2 * q) allows a plain scan per query. A hash map from allocation ID to start and length is all you need for erases. Segment trees are overkill and add bug risk.
What edge cases break a naive solution?+
Erasing an ID that never existed, erasing the same ID twice, trying to erase initially occupied cells, and failed allocations that must not consume an ID. Also an x bigger than the array. Test Example 2 by hand first.
How do I prep for this in 48 hours?+
Write the simulation from scratch twice and trace both examples. Practice the sliding zero-run pattern and map bookkeeping. Then test your own cases around reuse of freed space, since the leftmost block can move after an erase.