PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

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.

  • medium
  • NVIDIA
  • Coding & Algorithms
  • Software Engineer

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.

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: - `create` adds a new VM. If the id already exists, return the error string `'error: duplicate id'`. - `update` modifies an existing VM. If the id does not exist, return `'error: not found'`. - `delete` removes an existing VM. If the id does not exist, return `'error: not found'`. - `list` returns a snapshot of all current VMs sorted by `id` in 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_fields` will not contain the `id` key
  • All operations are one of: create, list, update, delete

Examples

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',)]

Expected 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.

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',)]

Expected 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.

Input: []

Expected Output: []

Explanation: No operations means no results.

Input: [('list',), ('create', {'id': 10, 'name': 'cache', 'cpu_cores': 1, 'memory_gb': 2, 'status': 'stopped'}), ('delete', 10), ('create', {'id': 10, 'name': 'cache-new', 'cpu_cores': 2, 'memory_gb': 4, 'status': 'running'}), ('list',), ('update', 10, {'cpu_cores': 4, 'status': 'stopped'}), ('list',)]

Expected Output: [[], 'created', 'deleted', 'created', [{'id': 10, 'name': 'cache-new', 'cpu_cores': 2, 'memory_gb': 4, 'status': 'running'}], 'updated', [{'id': 10, 'name': 'cache-new', 'cpu_cores': 4, 'memory_gb': 4, 'status': 'stopped'}]]

Explanation: Listing an empty manager returns an empty list. After deletion, the same id can be reused by a new VM.

Solution

def solution(operations):
    # Use a dictionary keyed by VM id so lookup, update, and delete are O(1) on average.
    # For 'list', return a sorted snapshot so the output order is deterministic.
    vms = {}
    results = []

    for op in operations:
        action = op[0]

        if action == 'create':
            vm = dict(op[1])
            vm_id = vm['id']

            if vm_id in vms:
                results.append('error: duplicate id')
            else:
                vms[vm_id] = vm
                results.append('created')

        elif action == 'list':
            # Copy each VM so earlier snapshots are not affected by later updates.
            snapshot = [vms[vm_id].copy() for vm_id in sorted(vms)]
            results.append(snapshot)

        elif action == 'update':
            vm_id, updated_fields = op[1], op[2]

            if vm_id not in vms:
                results.append('error: not found')
            else:
                vms[vm_id].update(updated_fields)
                results.append('updated')

        elif action == 'delete':
            vm_id = op[1]

            if vm_id not in vms:
                results.append('error: not found')
            else:
                del vms[vm_id]
                results.append('deleted')

    return results

Time complexity: Average O(1) for each create, update, and delete. Each list operation is O(n log n) because VMs are sorted by id before returning.. Space complexity: O(n), where n is the number of VMs currently stored..

Hints

  1. A hash map / dictionary keyed by VM id gives efficient average-time create, update, and delete.
  2. For deterministic `list` output, think about returning VMs in a fixed order rather than relying on hash map iteration order.
Last updated: May 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute the Final Robot Score - NVIDIA (easy)
  • Return all file paths via DFS - NVIDIA (easy)
  • Implement a disk space manager with eviction - NVIDIA (medium)
  • Implement encode/decode for list of strings - NVIDIA (easy)
  • Implement short algorithms on logs, grids, and strings - NVIDIA (hard)