When performing CRUD operations on a database, which operation can be the most expensive in terms of performance?

  • Create
  • Read
  • Update
  • Delete
Among CRUD operations, "Update" can often be the most expensive in terms of performance. This is because updating records may require the database to search for the existing record, make changes, and write the updated data back to disk, which can be resource-intensive. Read operations are typically less expensive.

When a Promise is pending and neither fulfilled nor rejected, it is in the ________ state.

  • awaiting
  • undefined
  • completed
  • idle
When a Promise is pending and hasn't been resolved or rejected, it is in the "awaiting" state. This is the initial state of a Promise before it is settled.

What are the best practices for error handling in a large-scale Node.js application?

  • Avoiding error handling altogether for performance reasons.
  • Using global error handlers for unhandled errors.
  • Handling errors at the lowest level of code execution.
  • Logging errors but not taking any action.
In large-scale Node.js applications, best practices for error handling include handling errors at the lowest level of code execution, close to where they occur. This improves code maintainability and makes it easier to trace the source of errors. Avoiding error handling for performance reasons is a bad practice. Using global error handlers is suitable for unhandled errors, but it's essential to handle errors at the source first. Simply logging errors without taking corrective action is incomplete error handling.

You are building a RESTful API with Express to serve a mobile application. The mobile development team has asked for the ability to retrieve condensed responses to minimize data usage. How would you accommodate this request while maintaining the integrity of your API?

  • Create separate endpoints for condensed and full responses.
  • Use query parameters to allow clients to specify the response format.
  • Disable compression to send smaller payloads.
  • Use WebSocket instead of REST for real-time updates.
Using query parameters to allow clients to specify the response format is a common and RESTful approach to accommodating different client needs. Creating separate endpoints for each format can lead to redundancy and maintenance challenges. Disabling compression would likely increase, not decrease, data usage. Using WebSockets is for real-time communication and doesn't directly address response format concerns.

In what scenario would using Domain API be beneficial for error handling in Node.js?

  • When handling HTTP requests in Express.js.
  • When handling file I/O operations.
  • When creating a RESTful API.
  • When dealing with database connections.
The Domain API was deprecated in Node.js and is no longer recommended for use. It was originally designed to handle errors in scenarios like file I/O operations. However, it has been deprecated because it didn't provide a robust solution for error handling, and other mechanisms like try...catch and event listeners have become more standard for handling errors in modern Node.js applications. Using it is not advisable in any scenario.

When using a third-party storage service to store uploaded files, what is crucial to prevent unauthorized access?

  • Use predictable file names and URLs for easy access.
  • Share access credentials widely to simplify sharing files.
  • Implement proper access controls and use signed URLs or tokens.
  • Store files without any access restrictions for maximum accessibility.
When using a third-party storage service, it's crucial to prevent unauthorized access by implementing proper access controls and using mechanisms like signed URLs or tokens. This ensures that only authorized users can access the files while keeping them secure. Using predictable file names and URLs, sharing access credentials widely, or storing files without restrictions can lead to unauthorized access and security breaches.

What considerations should be made when implementing transactions in Sequelize for isolation and atomicity?

  • Choosing the appropriate isolation level
  • Ensuring that transactions are started manually
  • Avoiding the use of nested transactions
  • Setting the transaction mode to 'autocommit'
When implementing transactions in Sequelize for isolation and atomicity, you should consider choosing the appropriate isolation level based on your application's requirements. Transactions should typically be started manually, avoiding nested transactions for simplicity and maintainability. Setting the transaction mode to 'autocommit' would defeat the purpose of using transactions for atomicity.

You need to develop a function that takes an array of numbers and returns a new array containing only the unique numbers. What approach would you use to filter out the duplicates?

  • Using a for loop and checking uniqueness manually
  • Using Array.prototype.filter() and a custom filter function
  • Using Array.from(new Set(array))
  • Using Array.prototype.reduce() with a custom accumulator function
To filter out duplicate numbers in an array, you can use Array.from(new Set(array)). This creates a Set from the array (which only keeps unique values) and then converts it back to an array. It's a concise and efficient approach for this task.

How can you allocate a buffer of a specified size without initializing it in Node.js?

  • Buffer.alloc(size)
  • Buffer.create(size)
  • Buffer.new(size)
  • Buffer.allocate(size)
To allocate a buffer of a specified size without initializing it in Node.js, you should use the Buffer.alloc(size) method. It allocates a new buffer of the given size and initializes it with zeros. The other options are not valid methods for achieving this.

Which caching strategy involves keeping the most recently used items?

  • LRU (Least Recently Used)
  • FIFO (First In, First Out)
  • Random Replacement
  • LFU (Least Frequently Used)
The caching strategy that involves keeping the most recently used items is called LRU, which stands for "Least Recently Used." In LRU caching, when the cache is full and needs to make space for a new item, it removes the least recently accessed item. This strategy is often used to maximize cache efficiency.