Implement Memory Allocation and In-Memory Records
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates implementation skills in memory management and in-memory data modeling, covering first-fit allocation semantics, free/merge behavior, CRUD operations for user records, and hierarchical traversal of reporting relationships.
Part 1: First-Fit Memory Allocator Simulation
Constraints
- 1 <= n <= 10^5
- 0 <= len(operations) <= 10^5
- For allocation operations, value >= 1
- For free operations, value is an integer start index
- All operations must be processed online in the given order
Examples
Input: (10, [[1, 3], [1, 4], [2, 0], [1, 2], [1, 3], [2, 3], [1, 5]])
Expected Output: [0, 3, 1, 0, 7, 1, 2]
Explanation: After freeing the block at 0, allocation reuses that space. Later freeing the block at 3 merges adjacent free space, allowing a size-5 allocation at index 2.
Input: (5, [[1, 5], [1, 1], [2, 4], [2, 0], [2, 0]])
Expected Output: [0, -1, 0, 1, 0]
Explanation: The whole memory is allocated first. A second allocation fails. Freeing start 4 fails because no block starts there. Freeing start 0 succeeds once, then fails the second time.
Input: (1, [[1, 1], [2, 0], [1, 1]])
Expected Output: [0, 1, 0]
Explanation: Single-unit memory can be allocated, freed, and reused.
Input: (8, [])
Expected Output: []
Explanation: Edge case with no operations.
Hints
- Keep a sorted list of free intervals and scan it from left to right to implement first-fit.
- Store allocated blocks by their starting index so that free(start) can verify validity and know the block size.
Part 2: In-Memory Record Store CRUD
Constraints
- 0 <= len(operations) <= 10^5
- 1 <= length of each name and email <= 200
- Names and emails do not contain the '|' character
- Ids generated by CREATE start at 1 and increase by 1
- Average-case O(1) map operations are expected
Examples
Input: ([['CREATE', 'Alice', 'alice@example.com'], ['GET', '1'], ['UPDATE', '1', 'Alice Smith', 'alice.smith@example.com'], ['GET', '1'], ['DELETE', '1'], ['GET', '1']],)
Expected Output: ['1', '1|Alice|alice@example.com', 'UPDATED', '1|Alice Smith|alice.smith@example.com', 'DELETED', 'NOT_FOUND']
Explanation: Basic create, read, update, delete flow on a single record.
Input: ([['CREATE', 'A', 'a@x.com'], ['CREATE', 'B', 'b@x.com'], ['GET', '2'], ['UPDATE', '3', 'C', 'c@x.com'], ['DELETE', '2'], ['GET', '2']],)
Expected Output: ['1', '2', '2|B|b@x.com', 'NOT_FOUND', 'DELETED', 'NOT_FOUND']
Explanation: Shows multiple records, lookup by id, and failure when updating a non-existent record.
Input: ([],)
Expected Output: []
Explanation: Edge case with no operations.
Input: ([['CREATE', 'Solo', 's@x.com'], ['DELETE', '1'], ['DELETE', '1'], ['GET', '1']],)
Expected Output: ['1', 'DELETED', 'NOT_FOUND', 'NOT_FOUND']
Explanation: Deleting the same record twice should only succeed once.
Hints
- A hash map from id to record gives fast GET, UPDATE, and DELETE.
- Keep a separate next_id counter. Do not use the current number of records, because deleted ids must not be reused.
Part 3: Employee Hierarchy Traversal
Constraints
- 0 <= len(employees) <= 10^5
- Employee ids are distinct integers
- manager_id is either -1 or the id of another employee
- The hierarchy contains no cycles
- If employee_id is absent from employees, return []
Examples
Input: ([[1, -1], [2, 1], [3, 1], [4, 2], [5, 2], [6, 3]], 1)
Expected Output: [2, 3, 4, 5, 6]
Explanation: Employee 1 manages 2 and 3 directly, and 4, 5, 6 indirectly.
Input: ([[1, -1], [2, 1], [3, 1], [4, 2], [5, 2], [6, 3]], 2)
Expected Output: [4, 5]
Explanation: Employee 2 has two direct reports and no deeper descendants.
Input: ([[1, -1], [2, 1]], 2)
Expected Output: []
Explanation: Edge case where the employee exists but has no reports.
Input: ([[1, -1], [2, 1]], 7)
Expected Output: []
Explanation: Edge case where the requested employee id does not exist.
Input: ([[10, -1]], 10)
Expected Output: []
Explanation: Single-employee company.
Hints
- Build an adjacency list from manager to direct reports, then run DFS or BFS starting from the given employee.
- Collect every reachable subordinate, and sort once at the end if the required output order is ascending.