Skip to content

Instantly share code, notes, and snippets.

@ekampf
Last active November 1, 2022 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ekampf/3b26db03b30f7e7b1aa6162c81017675 to your computer and use it in GitHub Desktop.
Save ekampf/3b26db03b30f7e7b1aa6162c81017675 to your computer and use it in GitHub Desktop.
Eran's Interview Question
// Implement a virtual memory manager that takes a large contiguous block of memory and manages allocations
// and deallocations on it.
// The entire buffer needs to be available for allocation. You can use whatever extra memory you need to manage it.
// You do need to consider the case of fragmentation.
//
// Example test case:
// - initialize a buffer of 5 chars: -----
// - allocate 5 blocks of 1 char: XXXXX
// - free the 2nd and 4th: X-X-X
// - Can you now call Alloc(2) ? What would you need to change in the interface to be able to do so?
//
// Clearly document design choices, algorithm and possible optimizations.
// While we require you to implement one memory allocation algorithm,
// also document future looking design considerations.
// There are many ways to implement this memory manager. It is important for us to know why you implemented it the way you did,
// whats the pros and cons to your implementation, etc.
//
// While the interface below is written in C++, feel free to implement the MemoryManager in the language of your choosing.
// If you need to change the interface due to language limitations (for example, not all languages have pointers)
// or for any other reason - please explain your reasoning for the change.
// For example:
// - if we wanted to use a different memory management strategy, how would we make that change.
// - is your solution thread-safe? whaat would it take to make it thread safe?
//
// *** Requirements ***
// 1. Working code (obviously).
// 2. Unit tests (using a unit testing library of your choosing)
// 3. Documentation (as describe in the 2nd paragraph above)
//
class MemoryManager {
public:
// buffer is a large chunk of contiguous memory.
// num_bytes is the size of the buffer.
MemoryManager(char * buffer, int num_bytes);
// Allocate memory of size 'size'. Use malloc() like semantics.
char* Alloc(int size);
// Free up previously allocated memory. Use free() like semantics.
void Free(char*);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment