Posts

Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we will create 2 pointers : left (starting from 0

Diagonal Traversal of Matrix in Javascript

Image
Diagonal Traversal of Matrix/2D Array in Javascript Problem statement:   In this post, we will traverse a given matrix/2D array diagonally (from center to top right corner). Refer below attached diagram for better understanding. Diagonal pattern: Expected Output: expected   output :     1     6     11     16     2     7     12     3     8     4 Input Matrix:  let   input2DArray  = [   [ 1 ,  2 ,  3 ,  4 ],   [ 5 ,  6 ,  7 ,  8 ],   [ 9 ,  10 ,  11 ,  12 ],   [ 13 ,  14 ,  15 ,  16 ], ]; Solution Approach :  When we think about the pattern required, we can confirm that there will always be n number of diagonals that we will be traversing, that means the outer loop will be from 0 to n-1.  I have added below diagram for a matrix n x n: Inner loop will be having 2 variables i and j as row and col of matrix. we can see that row index=i in each iteration starts from zero and j will start from the index= diagonal. Let's write the code now: let   n  =  input2DArray . length ; for  ( let  

Hackerearth Arrays Problem: Most Frequent

Hackerearth   Problem and solution in Javascript Problem statement:   Mr. Bourneville loves programming and he likes to face new programming challenges. After completing many challenges he has now given you one challenge which is one of his favorites. He has given you a list of N random integers and he wants you to find the integer which has the maximum frequency in the given list. Mr. Bourneville already has a solution for this but he is not satisfied with his solution. He wants you to write a shortest possible code for this task.  In case Frequency of two numbers is same print the smaller one Input: First line of input contains N, number of integers. Second line will contains N spaces separated integers. Output: Print the most frequent integer. Contraints: 3<=N<=10 4 -10 6 <=Integer<=10 6 Sample Input: 5 1 1 1 2 2 Sample Output: 1 Explanation Clearly, count of 1 more than count of 2. Problem link: click here . Solution Approach: 1). we will sort the array in ascending ord

Flatten array in Javascript

In this post, we will see how we can flatten an array in Javascript. This was asked to me in an interview. Problem Statement: Input array:  let   arr  = [ 1 ,  2 ,  3 , [ 4 ,  5 ], [[ 6 ,  7 ],  8 ]]; Expected Output: [ 1, 2, 3, 4, 5, 6, 7, 8 ] Approach 1: Using Array.flat() Use the Array.flat() method to get the desired output. The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. We will use "Infinity" as the depth so that it will merge all the nested elements. Refer below screenshot for the output. console . log ( arr . flat ( Infinity )); Output : [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ] But what if you are not allowed to use the inbuilt method in the interview. We can use something like approach 2 for that. Approach 2: Without Array.flat() we can use a recursive function. It will have a for each method that will act on each element of the parent array and if the element is found to be a nested array

Javascript Codewars Problem: "Highest Rank Number in an Array"

  Codewars   Problem and solution in Javascript Objective:   In this challenge, we will solve the “ Highest Rank Number in an Array ”   Codewars  Kata  using  javascript .   Problem statement:   Complete the method which returns the number which is most frequent in the given input array. If there is a tie for most frequent number, return the largest number among them.   Examples:   Input  :  [12, 10, 8, 12, 7, 6, 4, 10, 12] Output  :  12 Input  :  [12, 10, 8, 12, 7, 6, 4, 10, 12, 10] Output :   12 Input  : [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10] Output  :  3 Solution Approach :  we will create a hash-map/object from the given array and store the array elements as key of object and number of element's occurrence as value of that key in object.  For understanding hash-map, refer the blog  post . Once done with the object, we will traverse through the object to check which key has highest value and in case of equal values we need to check whose key is higher and accordingly retur

Create Telegram Bot using Nodejs in 10 minutes

Have you ever thought of creating a telegram bot for sending some useful information, news, alerts, images or even reminders, but didn't know where to start from. Well, in this tutorial we will create a Telegram BOT and will send a chat message from the BOT to user, and NodeJS will be used for the coding part. The best part is i will share the exact simple steps that you need to perform and it won't take more than 10 mins to setup your first telegram bot and to send first message to the user from the NodeJS code. Step 1: Create your first Telegram Bot Telegram has a bot to create the bots, just search the BotFather  and then follow the instructions to create a BOT. Use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot. The name of your bot is displayed in contact details and elsewhere. The Username is a short name, to be used in mentions and t.me links. The token is a string along t