Occupy and Clean Memory by Allocation ID
Updated
Problem statement
You are given a binary array memory, where 0 is free and 1 is initially occupied. Process each row in queries:
[0, k]: reserve the firstkcells of the leftmost free run having length at leastk. A successful allocation receives and returns the next positive allocation ID; failure returns -1 and does not consume an ID.[1, id]: free exactly the cells owned by the active allocationidand return its length. Return -1 for an unknown or already-cleaned ID.
Initially occupied cells belong to no allocation and cannot be cleaned.
Function
processMemoryAllocations(memory: int[], queries: int[][]) → int[]Examples
Example 1
memory = [0,0,1,0,0]queries = [[0,2],[0,1],[1,1],[0,2]]return = [1,2,2,3]The successful allocations receive IDs 1, 2, and 3; cleaning ID 1 frees two cells.
Example 2
memory = [1,0,0]queries = [[1,1],[0,3],[0,2],[1,1],[1,1],[0,3]]return = [-1,-1,1,2,-1,-1]Only the two-cell allocation succeeds. Its first cleanup frees two cells; repeated cleanup fails.
Constraints
1 <= memory.length,queries.length <= 2000memory[i]is 0 or 1.- Every query has exactly two integers and follows one of the stated forms.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Process Banking OperationsVercel · OA
- First Completed Mingo LineEpic · OAEpic interview questions
- Count House Segments After Sequential DestructionByteDance · OAByteDance interview questions
- HTTP Request RedirectionAmazon · OA
- Hungarian Lottery Winner CountsAlpaca · OA
- Cheating CodemakerJane Street · OAJane Street interview questions
- Aggregate Paginated Project Run RecordsReplit · OA
- Maximum Satisfied CustomersAtlassian · OAAtlassian interview questions