Feature #1: Allocate Space

Implementing the "Allocate Space" feature for our "Operating System" project.

Description

The first feature we need to build will identify the contiguous sub-processes that can be replaced with an incoming process of size n. In this scenario, we have p running processes numbered 1 through p in our system. We allocate memory to these processes contiguously such that process 1 occupies p1 MB, process 2 immediately occupies p2 MB, and so on. We receive the contiguous allocations p1, p2, …, pN as an array. An incoming process requires a contiguous chunk of n MB anywhere in memory. To allocate memory to this process, we can evict one process or a set of processes that occupy contiguous memory locations that once evicted free up an n MB amount of space collectively. Here, our task will be to find the total number of all possible subsets of currently running processes that together account for a contiguous allocation of n MBs of memory.

In this scenario, we will be provided with two inputs. First, we will be given an array of integers representing the size of contiguous memory chunks allocated to the running processes. Second, we’ll be given n, which is the size of the memory required for the new process (in MB).

Here is an illustration of this behavior:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.