TCS Node JS interview questions

By | 8 months ago

nodeinterviewjobsjavascript backendcareers jskochi kerala
  1. **Flattening an array, removing duplicates, and sorting it in JavaScript:**

Here's a JavaScript program that flattens an array, removes duplicates, and then sorts it:

const arr = [1,2,3,4,5,[4,65,4,8, [43,21,67]]]; function flattenAndSortArray(array) { // Flatten the array const flattened = array.flat(Infinity); // Remove duplicates const unique = [...new Set(flattened)]; // Sort the array const sorted = unique.sort((a, b) => a - b); return sorted; } console.log(flattenAndSortArray(arr));

This function first flattens the array using `flat(Infinity)` to handle deep nesting, then removes duplicates by converting the array to a Set, and finally sorts the array numerically.

  1. **Clusters in Node.js:**

Clusters in Node.js refer to a module that allows you to create child processes that run simultaneously and share the same server port. It's a way to take advantage of multi-core systems, allowing you to fork multiple instances of your Node.js application, thereby improving the performance and handling of the load. Each instance can run on a separate CPU core, enabling better resource utilization and scalability.

  1. **Priority of execution: callback, promise, setTimeout, process.nextTick():**

In the Node.js event loop, the priority of execution is as follows:

  • process.nextTick(): This has the highest priority and executes immediately after the current operation completes, even before any I/O events or timers.

  • `Promise: Promises are resolved in the microtask queue, which is processed after the current operation completes and after process.nextTick()` callbacks, but before any I/O events or timers.

  • `setTimeout: Code inside setTimeout` is placed in the timer queue and executed after the specified delay, making it execute after the microtask queue and any I/O events if the delay has elapsed.

  • Callback: This depends on the context. If it's an I/O callback, it's executed in the subsequent phases of the event loop after timers and I/O callbacks.

  1. **When to choose Node.js for creating a backend server:**

You might choose Node.js over languages like Java or Python when:

  • You need a non-blocking, event-driven server, especially for I/O-heavy operations.

  • You're building real-time applications (like chat apps or live updates).

  • You want to use JavaScript on both the frontend and backend.

  • You need fast and scalable network applications.

  • However, if your application requires heavy computational tasks, Node.js might not be the best choice due to its single-threaded nature.

  1. **RxJS ReplaySubject:**

In RxJS, a `ReplaySubject` is a type of subject that can send old values to new subscribers. It can store and replay `n` number of emitted values to all subscribers by caching the values. This is particularly useful when you need late subscribers to receive previous values. The `ReplaySubject` can be configured to hold a specific number of the most recent values and can also be set to replay values emitted within a certain time window.