Coding questions · NVIDIA
Implement simple VM manager with CRUD operations
This question evaluates a candidate's ability to design and implement in-memory data models and CRUD APIs with unique-identifier management and consistent error signaling, testing skills in data structures, API/interface design, and algorithmic complexity.
You are asked to implement a simple in-memory virtual machine (VM) manager that can:
- List all VMs
- Add (create) a new VM
- Modify (update) an existing VM
- Delete an existing VM
Each VM has a set of attributes. For this exercise, you can assume at least the following attributes:
id(string or integer, must be unique)name(string)cpu_cores(integer)memory_gb(integer)status(string, e.g.,"running","stopped")
Requirements
-
Data model & storage
- Choose an appropriate in-memory data structure to store the VMs.
- Ensure
idis treated as the unique identifier for each VM.
-
API / Interface Design and implement a small API (in the language of your choice) with at least the following operations:
-
create_vm(vm)- Input: VM attributes (including
id). - Behavior: Adds a new VM to the manager.
- Input: VM attributes (including
-
list_vms()- Input: none.
- Output: a collection/list of all VMs currently stored.
-
update_vm(id, updated_fields)- Input: the
idof the VM to update, and a set/dictionary of fields to update. - Behavior: Modifies the attributes of the existing VM with the given
id.
- Input: the
-
delete_vm(id)- Input: the
idof the VM to delete. - Behavior: Removes the VM with the given
idfrom the manager.
- Input: the
-
-
Error handling
- If
create_vmis called with anidthat already exists, you must detect this and handle it as an error. - If
update_vmordelete_vmis called with anidthat does not exist, you must detect this and handle it as an error. - Define clearly how errors are surfaced (e.g., exceptions, error codes, or special return values) and keep it consistent across methods.
- If
-
Complexity considerations
- Aim for efficient average-time operations for lookup, update, and delete by
id.
- Aim for efficient average-time operations for lookup, update, and delete by
Explain your design choices (data structures and API shape) in brief comments or docstrings within your code.
Implement Simple VM Manager with CRUD Operations
You are given a sequence of API calls to an in-memory virtual machine (VM) manager. Process the calls in order and return the result of each operation.
Each VM has these fields:
- id: integer, unique among currently stored VMs
- name: string
- cpu_cores: integer
- memory_gb: integer
- status: string
Implement a function solution(operations) where each operation is one of:
('create', vm_dict)('list',)('update', id, updated_fields_dict)('delete', id)
Rules:
createadds a new VM. If the id already exists, return the error string'error: duplicate id'.updatemodifies an existing VM. If the id does not exist, return'error: not found'.deleteremoves an existing VM. If the id does not exist, return'error: not found'.listreturns a snapshot of all current VMs sorted byidin ascending order.
Return a list containing the result of every operation in the same order:
'created'for a successful create'updated'for a successful update'deleted'for a successful delete- a list of VM dictionaries for a list operation
- the exact error strings above for failures
Include brief comments in your code explaining your data structure choice.
Constraints
- 0 <= len(operations) <= 10^4
- All VM ids are integers
- Each create operation provides all required fields: id, name, cpu_cores, memory_gb, status
- For this problem,
updated_fieldswill not contain theidkey - All operations are one of: create, list, update, delete
Examples
Example 1
Input: [('create', {'id': 2, 'name': 'db', 'cpu_cores': 4, 'memory_gb': 16, 'status': 'running'}), ('create', {'id': 1, 'name': 'web', 'cpu_cores': 2, 'memory_gb': 8, 'status': 'stopped'}), ('list',), ('update', 1, {'status': 'running', 'memory_gb': 12}), ('list',), ('delete', 2), ('list',)]
Output: ['created', 'created', [{'id': 1, 'name': 'web', 'cpu_cores': 2, 'memory_gb': 8, 'status': 'stopped'}, {'id': 2, 'name': 'db', 'cpu_cores': 4, 'memory_gb': 16, 'status': 'running'}], 'updated', [{'id': 1, 'name': 'web', 'cpu_cores': 2, 'memory_gb': 12, 'status': 'running'}, {'id': 2, 'name': 'db', 'cpu_cores': 4, 'memory_gb': 16, 'status': 'running'}], 'deleted', [{'id': 1, 'name': 'web', 'cpu_cores': 2, 'memory_gb': 12, 'status': 'running'}]]
Explanation: The manager stores VMs by id. Listing is sorted by id, so VM 1 appears before VM 2 even though VM 2 was created first.
Example 2
Input: [('create', {'id': 1, 'name': 'vm1', 'cpu_cores': 1, 'memory_gb': 2, 'status': 'running'}), ('create', {'id': 1, 'name': 'dup', 'cpu_cores': 2, 'memory_gb': 4, 'status': 'stopped'}), ('update', 3, {'status': 'stopped'}), ('delete', 2), ('list',)]
Output: ['created', 'error: duplicate id', 'error: not found', 'error: not found', [{'id': 1, 'name': 'vm1', 'cpu_cores': 1, 'memory_gb': 2, 'status': 'running'}]]
Explanation: Creating a VM with an existing id fails. Updating or deleting a missing id also fails.
Hints
- A hash map / dictionary keyed by VM id gives efficient average-time create, update, and delete.
- For deterministic
listoutput, think about returning VMs in a fixed order rather than relying on hash map iteration order.