Design Memory Allocator - Problem

You are given an integer n representing the size of a 0-indexed memory array. All memory units are initially free.

You have a memory allocator with the following functionalities:

  • Allocate a block of size consecutive free memory units and assign it the id mID.
  • Free all memory units with the given id mID.

Note that:

  • Multiple blocks can be allocated to the same mID.
  • You should free all the memory units with mID, even if they were allocated in different blocks.

Implement the Allocator class:

  • Allocator(int n) Initializes an Allocator object with a memory array of size n.
  • int allocate(int size, int mID) Find the leftmost block of size consecutive free memory units and allocate it with the id mID. Return the block's first index. If such a block does not exist, return -1.
  • int freeMemory(int mID) Free all memory units with the id mID. Return the number of memory units you have freed.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["Allocator", "allocate", "allocate", "allocate", "freeMemory"], values = [[10], [1, 1], [1, 2], [1, 3], [2]]
Output: [null, 0, 1, 2, 1]
💡 Note: Initialize allocator with size 10. allocate(1,1) returns 0 (first position). allocate(1,2) returns 1. allocate(1,3) returns 2. freeMemory(2) frees 1 unit and returns 1.
Example 2 — Failed Allocation
$ Input: operations = ["Allocator", "allocate", "allocate", "allocate"], values = [[2], [1, 1], [1, 2], [1, 3]]
Output: [null, 0, 1, -1]
💡 Note: Memory size is 2. First two allocations succeed at positions 0 and 1. Third allocation fails since no free space exists, returns -1.
Example 3 — Multiple Blocks Same ID
$ Input: operations = ["Allocator", "allocate", "allocate", "freeMemory"], values = [[10], [2, 1], [3, 1], [1]]
Output: [null, 0, 2, 5]
💡 Note: allocate(2,1) uses positions [0,1]. allocate(3,1) uses positions [2,3,4]. freeMemory(1) frees all 5 positions allocated to mID=1.

Constraints

  • 1 ≤ n, size ≤ 1000
  • 1 ≤ mID ≤ 1000
  • At most 1000 calls will be made to allocate and freeMemory

Visualization

Tap to expand
Design Memory Allocator INPUT Memory Array (n=10) 0 1 2 3 4 5 6 7 8 9 All free (0) Operations: 1. Allocator(10) 2. allocate(1, 1) 3. allocate(1, 2) 4. allocate(1, 3) 5. freeMemory(2) Uses: Array + HashMap memory[] mID map ALGORITHM STEPS 1 Initialize Create array[10] with 0s 2 allocate(1,1) Find leftmost free, mark [0]=1 1 ... 3 allocate(1,2) + allocate(1,3) Mark [1]=2, [2]=3 1 2 3 ... 4 freeMemory(2) Find all mID=2, set to 0 1 0 3 ... HashMap Tracking: mID 1 --> [0] mID 2 --> [1] (freed) mID 3 --> [2] FINAL RESULT Final Memory State: 1 0 3 0 0 0 0 0 0 0 Operation Results: Allocator(10) --> null allocate(1,1) --> 0 allocate(1,2) --> 1 allocate(1,3) --> 2 freeMemory(2) --> 1 Output: [null, 0, 1, 2, 1] Key Insight: Use an array to track memory state (0=free, mID=allocated) and scan left-to-right for allocation. HashMap maps mID to indices for O(1) lookup during free operations. Time: O(n) allocate, O(k) free. TutorialsPoint - Design Memory Allocator | HashMap + Array Tracking
Asked in
Amazon 15 Microsoft 8 Google 5
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen