Javascript Articles

Page 151 of 534

Checking for convex polygon in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 513 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 249 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 355 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 892 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 324 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 390 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 368 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

Bring number down to 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 129 Views

Problem We need to write a JavaScript function that takes a number and finds the minimum operations to reduce it to 1 using these rules: If the number is even, divide it by 2 If the number is odd, add 1 or subtract 1 The function should return the minimum number of operations required. Example For input number 7, the output should be 4 operations: Input: 7 Output: 4 Output Explanation The optimal path requires 4 steps: 7 → 8 → 4 → 2 → 1 ...

Read More

Nth smallest element in sorted 2-D array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 435 Views

In JavaScript, finding the Nth smallest element in a sorted 2D array requires an efficient approach. We'll use binary search on the value range rather than indices. Problem Statement Given a sorted 2D array (sorted in increasing order both row-wise and column-wise), find the Nth smallest element. const arr = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ]; For example, if we want the 5th smallest element from the above array, the answer would be 11. Approach: Binary Search on Values ...

Read More

Any possible combination to add up to target in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 392 Views

We are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument. Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count. Problem Example For example, if the input to the function is: const arr = [1, 2, 3]; const target = 4; Then the output should be: 7 Output Explanation The possible combination ways are: ...

Read More
Showing 1501–1510 of 5,340 articles
« Prev 1 149 150 151 152 153 534 Next »
Advertisements