← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
You are given an integer n representing the size of a 0-indexed memory array. All memory units are initially free (unallocated). You must implement a memory allocator that supports two operations:
allocate(size, mID): Find the leftmost contiguous block of size consecutive free memory units (cells containing 0). If such a block exists, mark all of its units with the integer mID (to indicate they are now allocated) and return the starting index of the block. If no suitable block exists, return -1.
free(mID): Release every memory unit that is currently allocated with the given ID mID (i.e., set all cells whose value equals mID back to 0). Return the total number of memory units that were freed.
You should implement the Allocator class: