Web Development Articles

Page 249 of 801

Finding median for every window in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 942 Views

In mathematics, the median is the middle value in an ordered (sorted) list of numbers. If the list has an even number of elements, the median is the average of the two middle values. Problem Statement We need to write a JavaScript function that takes an array of integers and a window size, then calculates the median for each sliding window of that size. The function returns an array containing all the calculated medians. For example, given: const arr = [5, 3, 7, 5, 3, 1, 8, 9, 2, 4, 6, 8]; const windowSize = ...

Read More

Forming string using 0 and 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 322 Views

We need to write a JavaScript function that takes an array of binary strings and determines the maximum number of strings that can be formed using at most m zeros and n ones. Problem Statement Given an array of strings containing only '0' and '1', find how many strings can be selected such that the total count of zeros doesn't exceed m and the total count of ones doesn't exceed n. For example, with the array ["10", "0001", "111001", "1", "0"] and limits m=5 zeros and n=3 ones: Input: arr = ["10", "0001", "111001", "1", ...

Read More

Encoding string to reduce its size in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Problem We need to write a JavaScript function that takes a string and encodes it using a compression format. The function should compare the encoded string with the original and return whichever is smaller. The encoding rule is: n[s], where s inside the square brackets is being repeated exactly n times. For example, "ddd" can be encoded to "3[d]", but since "3[d]" has 4 characters while "ddd" has only 3, the function should return "ddd". Example Input and Output For the input string: 'aabcaabcd' The expected ...

Read More

Checking for convex polygon in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

A convex polygon is a polygon where all interior angles are less than 180°. In a convex polygon, all vertices point "outward" and no line segment between any two points inside the polygon extends outside the polygon. Problem Statement We need to write a JavaScript function that takes an array of coordinates representing vertices of a polygon. Each coordinate is an array containing two numbers [x, y] representing a point on a 2D plane. The function should return true if the polygon is convex, false otherwise. For example, if the input is: const arr = ...

Read More

Unique substrings in circular string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 255 Views

In JavaScript, we need to find the number of unique non-empty substrings of a given string that exist in an infinite wraparound string of the alphabet. The wraparound string is an infinite repetition of "abcdefghijklmnopqrstuvwxyz". Problem Statement Given a string S, which is an infinite wraparound string of: "abcdefghijklmnopqrstuvwxyz" The infinite string S looks like: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...." We need to find how many unique non-empty substrings of a given input string are present in this infinite wraparound string. Example For input string "zab": Input: "zab" Output: 6 ...

Read More

Is the string a combination of repeated substrings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 360 Views

We need to write a JavaScript function that checks if a string can be constructed by repeating a substring multiple times. This is useful for pattern detection in strings. Problem Statement Given a string, determine if it consists of a repeated pattern. For example, "abcabcabc" is made by repeating "abc" three times, while "abcdef" has no repeating pattern. Example Input and Output For the string: const str = 'thisthisthisthis'; The expected output is: const output = true; Because the string is constructed by repeating 'this' four times. ...

Read More

Sorting string characters by frequency in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 904 Views

In JavaScript, sorting string characters by frequency involves counting each character's occurrences and rearranging them from most frequent to least frequent. This technique is useful for data analysis, compression algorithms, and text processing tasks. Problem Statement We need to create a JavaScript function that takes a string as input and returns a new string where characters are arranged by their frequency in descending order. Characters appearing most frequently come first, followed by less frequent ones. For example, if the input string is: const str = 'free'; The expected output should be: ...

Read More

Deleting desired node from a Binary Search Tree in JavaScrip

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 332 Views

Deleting a node from a Binary Search Tree is more complex than insertion because we need to handle three different cases depending on the node's children. This operation maintains the BST property where left children are smaller and right children are larger. Problem We have a Binary Search Tree implementation with insertion functionality. We need to add a deleteNode() function that removes a node with a specific value while maintaining the BST structure. class Node { constructor(data) { this.data = data; ...

Read More

Finding Sum of Left Leaves of a BST in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 396 Views

We are required to write a JavaScript function that takes in the root of a Binary Search Tree as the only argument. The function should simply calculate the sum of data stored in the left leaves of the BST. Problem Example For example, if the Tree looks like this: 8 / \ 1 10 / \ 5 17 Then the output should be: 6 Output Explanation Because there are two left leaves in the ...

Read More

Smallest number after removing n digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

We need to write a JavaScript function that removes n digits from a number to make it as small as possible. This is a classic greedy algorithm problem that uses a stack-based approach. Problem Statement Given a number string m and an integer n, remove n digits from m to create the smallest possible number. For example: Input: m = '45456757', n = 3 Output: '44557' We remove digits 5, 6, and 7 to get the smallest result. Algorithm Approach We use a greedy algorithm with a stack: Process ...

Read More
Showing 2481–2490 of 8,010 articles
« Prev 1 247 248 249 250 251 801 Next »
Advertisements