Node Js Coding Questions and Answers
This article provides Node js coding questions and answers to help you better understand the basics of Node.js development. Get the answers you need to master Node.js coding and take your development skills to the next level.
Reverse a string:
Write a function to reverse a given string in Node.js.
Explanation: The reverseString function takes a string as input. It uses split('') to convert the string into an array of characters, then uses reverse() to reverse the order of the array, and finally uses join('') to convert the array back to a string with reversed characters.
Find the first non-repeated character:
Write a function to find the first non-repeated character in a given string in Node.js.
The findFirstNonRepeatedChar function uses an object charCount to keep track of the count of each character in the input string. It then iterates through the string twice. First, it counts the occurrences of each character and stores them in charCount. Second, it finds the first character with a count of 1 in charCount and returns it as the first non-repeated character.
Implement a stack:
Implement a stack (LIFO) data structure in Node.js with push, pop, and peek operations.
The Stack class uses an array items to store the elements in the stack. It has push to add an item to the top of the stack, pop to remove and return the top item, peek to return the top item without removing it, and isEmpty to check if the stack is empty.
Find the sum of two numbers in a sorted array:
Given a sorted array of integers, write a function to find two numbers that sum up to a target number in Node.js.
The findSumInSortedArray function uses a two-pointer approach with left and right pointers initially pointing to the start and end of the sorted array, respectively. It calculates the sum of the numbers at the left and right pointers, and compares it with the target sum. If the sum is equal to the target, it returns the numbers at left and right as the result. If the sum is less than the target, it moves the left pointer to the right to increase the sum, and if the sum is greater than the target, it moves the right pointer to the left to decrease the sum. This process continues until a pair of numbers with the target sum is found or the pointers meet, in which case it returns null to indicate that no such pair was found.
Check if a binary tree is a valid BST:
Given a binary tree, write a function to determine if it is a valid binary search tree (BST) in Node.js.
Explanation: The isValidBST function takes the root of a binary tree as input and calls the checkBST helper function to recursively check if each node in the tree satisfies the conditions of a valid BST. The checkBST function takes a node, along with a min and a max value, to represent the allowed range of values for that node. It returns true if the node is null (i.e., an empty tree is considered a valid BST), or if the node's value is within the allowed range and both its left and right subtrees are valid BSTs.
Find the longest increasing subarray:
Given an array of integers, write a function to find the length of the longest increasing subarray in Node.js.
The findLongestIncreasingSubarray function iterates through the input array arr and keeps track of the current length of the increasing subarray using the currentLength variable. When a number in the array is smaller than the next number, it increases currentLength by 1. When a number in the array is not smaller than the next number, it updates the maxLength with the maximum value between maxLength and currentLength + 1, and resets currentLength to 0. Finally, it returns the maxLength which represents the length of the longest increasing subarray.
Find the median of two sorted arrays:
Given two sorted arrays, write a function to find the median element in Node.js.
The `findMedianSortedArrays` function takes two sorted arrays, `nums1` and `nums2`, as input and merges them into a single sorted array called `merged`. It uses two pointers, `i` and `j`, to traverse `nums1` and `nums2` respectively, comparing elements at each step and pushing the smaller one into `merged`. After the merging process is complete, it calculates the median based on the length of the merged array. If the length is even, it returns the average of the middle two elements. If the length is odd, it returns the middle element.
Serialize and deserialize a binary tree:
Given a binary tree, write functions to serialize and deserialize it in Node.js.
The serialize function takes the root of a binary tree as input and serializes it into a comma-separated string representation. It recursively traverses the tree in a pre-order manner, appending the values of each node, along with the serialized left and right subtrees, to the result string.
The deserialize function takes a serialized string representation of a binary tree as input and deserializes it back into a binary tree object. It does so by splitting the input string into an array of values, and then recursively building the tree from the values using a helper function called buildTree. The buildTree function takes the array of values as input, and for each value, it creates a new node with that value, recursively builds its left and right subtrees, and returns the constructed node.
Given an array of intervals, write a function to merge overlapping intervals in Node.js.
The `mergeIntervals` function takes an array of `Interval` objects as input, where each `Interval` object represents an interval with a start and end time. It first checks if the input array has only one interval or is empty, in which case it directly returns the input array as is.
Next, it sorts the intervals based on their start times in ascending order. It then initializes an empty array called `mergedIntervals` to store the merged intervals. It also initializes a `currentInterval` variable to keep track of the interval being currently merged, starting with the first interval in the sorted array.
The function then iterates through the remaining intervals in the sorted array. For each interval, it checks if it overlaps with the current interval being merged. If it does, it updates the end time of the current interval being merged to be the maximum of the end times of both overlapping intervals. If it does not overlap, it pushes the current interval being merged into the `mergedIntervals` array and sets the current interval being merged to be the current interval in the sorted array.
After the loop, the last remaining interval being merged is pushed into the `mergedIntervals` array, and the final merged intervals are returned.
Implement LRU Cache:
Write a class for a Least Recently Used (LRU) Cache in Node.js.
The LRUCache class implements a Least Recently Used (LRU) cache using a combination of a Map and an internal capacity variable. The capacity parameter passed to the constructor determines the maximum number of key-value pairs that the cache can hold.
The class has two main methods, get and put, which implement the basic operations of a cache: retrieving a value by key, and adding or updating a key-value pair.
The get method first checks if the key exists in the cache. If it does, it returns the corresponding value and moves the accessed key-value pair to the end of the Map to mark it as the most recently used. If the key does not exist, it returns -1 to indicate that the key is not in the cache.
The put method first checks if the key already exists in the cache. If it does, it updates the value and moves the key-value pair to the end of the Map to mark it as the most recently used. If the key does not exist and the cache is already at its capacity, it removes the least recently used key-value pair from the beginning of the Map. Finally, it adds the new key-value pair