Implement simple VM manager with CRUD operations
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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
1. **Data model & storage**
- Choose an appropriate in-memory data structure to store the VMs.
- Ensure `id` is treated as the unique identifier for each VM.
2. **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.
- `list_vms()`
- Input: none.
- Output: a collection/list of all VMs currently stored.
- `update_vm(id, updated_fields)`
- Input: the `id` of the VM to update, and a set/dictionary of fields to update.
- Behavior: Modifies the attributes of the existing VM with the given `id`.
- `delete_vm(id)`
- Input: the `id` of the VM to delete.
- Behavior: Removes the VM with the given `id` from the manager.
3. **Error handling**
- If `create_vm` is called with an `id` that already exists, you must detect this and handle it as an error.
- If `update_vm` or `delete_vm` is called with an `id` that 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.
4. **Complexity considerations**
- Aim for efficient average-time operations for lookup, update, and delete by `id`.
Explain your design choices (data structures and API shape) in brief comments or docstrings within your code.
Quick Answer: 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.