← Questions

Simple VM manager with add, list, modify and delete

0
NvidiaCodingLast asked Nov 2025

A coding interview question asked in Nvidia's technical phone screen, last asked Nov 2025. It tests object-oriented design, crud and error handling.

Write a simple in-memory manager for virtual machines that can create, list, update and delete VMs. Each VM has an id (unique), name, cpu_cores, memory_gb and status (for example "running" or "stopped"). Errors must be detected and returned in one consistent way, and the code should be easy to extend.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • create_vm(vm: dict) -> str adds a VM; returns "created", or "error: duplicate id" if the id exists.
  • list_vms() -> list[dict] returns copies of all VMs sorted by id.
  • update_vm(id: int, fields: dict) -> str changes the given fields; returns "updated", or "error: not found".
  • delete_vm(id: int) -> str returns "deleted", or "error: not found".

Example

create_vm({"id": 2, "name": "db",  "cpu_cores": 4, "memory_gb": 16, "status": "running"})  ->  "created"
create_vm({"id": 1, "name": "web", "cpu_cores": 2, "memory_gb": 8,  "status": "stopped"})  ->  "created"
create_vm({"id": 1, "name": "dup", "cpu_cores": 2, "memory_gb": 4,  "status": "stopped"})  ->  "error: duplicate id"
update_vm(1, {"status": "running", "memory_gb": 12})  ->  "updated"
update_vm(3, {"status": "stopped"})                   ->  "error: not found"
delete_vm(2)                                          ->  "deleted"
list_vms()  ->  [{"id": 1, "name": "web", "cpu_cores": 2, "memory_gb": 12, "status": "running"}]

Constraints

  • Up to 10^4 operations; ids are integers.
  • update_vm never changes the id.

Clarifying questions

  • Should errors be exceptions, error codes or return values?
  • Should list_vms have a fixed order?
  • Should update_vm validate field names and values (negative cores, unknown status)?
  • Is thread safety needed?

What a strong answer covers

  • A dictionary keyed by id for average O(1) create, update and delete, and sorting (or an ordered map) for deterministic listing.
  • Returning copies so callers cannot mutate stored VMs behind the manager's back.
  • One consistent error style across all methods, and validation of inputs.
  • A structure that extends easily: a VM dataclass, a separate storage layer, room for new attributes and operations such as start and stop.
  • Tests for duplicates, missing ids, and update of several fields at once.

Follow-ups

  • Add start_vm and stop_vm with valid state transitions only.
  • Make the manager safe for concurrent callers.
  • Persist the VMs so they survive a restart.
  • Add filtering, such as all running VMs with at least 8 GB.

Where it's asked

What it tests

object-oriented design · crud · error handling

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding