Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 260 of 801
Adding and searching for words in custom Data Structure in JavaScript
We are required to design a data structure in JavaScript that supports two key operations: adding words and searching with pattern matching using regular expressions. Problem The data structure must support the following operations: addWord - adds a word to the data structure using arrays or any other storage mechanism search - searches for a literal word or a regular expression pattern containing lowercase letters "a-z" or "." where "." can represent any single letter Example Requirements addWord("sir") addWord("car") addWord("mad") search("hell") === false search(".ad") === true search("s..") === true ...
Read MoreLongest possible string built from two strings in JavaScript
We need to write a JavaScript function that takes two strings containing only letters from a to z and returns the longest possible sorted string with distinct letters from both strings combined. Problem Statement Given two strings s1 and s2, create a function that: Combines both strings Removes duplicate characters Returns a sorted string containing each unique letter only once Solution Using Array Methods Here's an implementation that combines the strings, removes duplicates, and sorts the result: const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', ...
Read MoreFinding life path number based on a date of birth in JavaScript
A person's Life Path Number is calculated by adding each individual digit in that person's date of birth, then reducing it to a single digit number through repeated digit summation. Problem We need to write a JavaScript function that takes a date in "yyyy-mm-dd" format and returns the life path number for that date of birth. How It Works The calculation involves three steps: Sum all digits in the year, month, and day separately Reduce each sum to a single digit by adding digits repeatedly Add the three single digits and reduce to final ...
Read MoreSmallest number formed by shuffling one digit at most in JavaScript
We need to write a JavaScript function that takes a positive number and can perform at most one operation: remove a digit from any position and insert it at another position to create the smallest possible number. Problem Statement Given a positive number, we can choose any digit, remove it from its current position, and insert it at any other position (including the same position). The goal is to find the smallest number possible with this single operation. Algorithm Approach The strategy is to find the smallest digit in the number and move it to the ...
Read MoreReturning the value of (count of positive / sum of negatives) for an array in JavaScript
Problem We are required to write a JavaScript function that takes in an array of integers (positives and negatives) and our function should return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. Using Array.reduce() Method The most efficient approach uses the reduce() method to iterate through the array once and accumulate both values simultaneously. const arr = [1, 2, 1, -2, -4, 2, -6, 2, -4, 9]; const posNeg = (arr = []) => { const result = arr.reduce((acc, ...
Read MoreValidating a boggle word using JavaScript
A Boggle board is a 2D array of individual characters. We need to validate whether a given word can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells. Problem Given a Boggle board like this: const board = [ ["I", "L", "A", "W"], ["B", "N", "G", "E"], ["I", "U", "A", "O"], ["A", "S", "R", "L"] ]; We need to check if words like "BINGO" or "LINGO" are valid. Valid words are ...
Read MoreNext multiple of 5 and binary concatenation in JavaScript
We need to write a JavaScript function that takes a number and finds the next higher multiple of 5 by concatenating the shortest possible binary string to the number's binary representation. Problem Given a number n, we want to: Convert n to binary representation Append the shortest binary string to make it divisible by 5 Return the resulting decimal number How It Works The algorithm generates all possible binary combinations of increasing length until it finds one that, when concatenated to the original number's binary form, creates a number divisible by 5. Example ...
Read MoreFinding the longest non-negative sum sequence using JavaScript
Problem We are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1. Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher. Understanding the Algorithm The solution uses a prefix sum approach with an index mapping technique. It tracks cumulative sums and uses an array to store the first occurrence of each sum value, allowing efficient calculation of subarray lengths. Example Following is the ...
Read MoreFinding all solutions of a Diophantine equation using JavaScript
Problem We need to write a JavaScript function that takes a number n and finds all integer solutions (x, y) for the Diophantine equation: x^2 - 4y^2 = n The function should return an array of all such pairs [x, y]. Mathematical Approach To solve x² - 4y² = n, we can factorize it as: x^2 - 4y^2 = (x + 2y)(x - 2y) = n Let a = x - 2y and b = x + 2y, then: a × b = n x = (a + ...
Read MoreFinding one missing number in a scrambled sequence using JavaScript
We are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n. The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array. Problem Statement Given an array of numbers from 1 to n with one missing number, find the missing number. The array is not sorted and contains n-1 elements instead of n. Using Sum Formula Method The most efficient approach uses the mathematical formula ...
Read More