FastPrepCircular Memory Slot Allocator

Circular Memory Slot Allocator

ZipRecruiter logoZipRecruiter● MediumFULLTIMENEW GRADOA
Learn

Problem statement

Memory contains totalSlots slots in a circle. Each request is [kind,start,length].

  • Save scans candidate starts cyclically beginning at start and finds the first run of length consecutive free slots, allowing the run to wrap. Occupy it and return its starting slot, or return -1 without mutation.
  • Clear frees the guaranteed-occupied circular run beginning at start and returns length.

Return one result per request.

Function

processMemoryRequests(totalSlots: int, requests: String[][]) → int[]

Examples

Example 1

totalSlots = 5requests = [["Save","1","2"],["Save","1","2"],["Clear","1","2"],["Save","1","2"]]return = [1,3,2,1]

Allocation, first-fit movement, clearing, and reuse are exercised.

Example 2

totalSlots = 5requests = [["Save","4","2"]]return = [4]

A run may wrap from the final slot to zero.

Constraints

  • 1 <= totalSlots <= 1000
  • 1 <= requests.length <= 100000
  • Every length is between 1 and totalSlots.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[] processMemoryRequests(int totalSlots, String[][] requests) {
    // Write your code here.
}
totalSlots5
requests[["Save","1","2"],["Save","1","2"],["Clear","1","2"],["Save","1","2"]]
expected[1,3,2,1]
Checking account…