(50+) JavaScript Coding DSA Questions for Freshers

JavaScript Coding DSA Questions for Freshers

If you are preparing for an IT Developer job then you must do DSA and coding preparation, in the interview selection process you must go through the coding around or DSA round.

Here I have given you the best JavaScript coding DSA questions for freshers, you must go through these questions before your coding around the interview, these questions help me a lot and I make sure discussions also help you to clear your coding rounds.

JavaScript Coding DSA Questions for Freshers

Here I have given 50+ best JavaScript coding DSA questions for freshers according to topic, you must go through these questions and try your own.

Basic JavaScript DSA Questions

how to delete an element from the array using data structures?

let data = [89,35,63,74,73,84,81,39,62]
let position = 0;
console.log(data);
for(let i =position; i<data.length-1; i++){
    data[i]=data[i+1]
}
data.length = data.length-1

console.log(data);

How to reverse an array?

let arr = [1,2,3,4,5,6,7]
let arr1 = []
for (let i = arr.length -1; i >= 0; i--) {
    arr1.push(arr[i]);
}
console.log(arr1,arr);

What is the logic behind push() and pop() functions?

let arr = []
let len = arr.length;

function push(val){
    arr[len]=val;
    len++
}

push(10);
push(58);
push(16);
console.log(arr);

function pop() {
    if(len>0){
        len = len-1
        arr.length = len
    }else{
        alert('Array is already Empty')
    }
}

pop()
console.log(arr);

How to search element position in an array?

let data = [89,35,63,74,73,84,81,39,62]
let item = 84;
let index = undefined;

for(let i =0; i<data.length-1; i++){
    if(data[i]==item){
        index = i
    }    
}
console.log(index);

How to merge two arrays?

let data = [89,35,63,74,73,84,81,39,62]
let data1 = [2,4,65,87,45,76]
let data2 = [];

// using Shortcut Method Push Method
for(let i=0; i<data.length;i++){
    data1.push(data[i])
}
console.log(data1);

//Using Main Method

for(let i=0; i<data.length;i++){
    data2[i]=data[i]
}
for(let i=0; i<data1.length;i++){
    data2[data.length+i]=data1[i]
}
console.log(data2);

How to check the Palindrome string?

let str = 'levely';
let start =0;
let end = str.length-1;
let result = true;
function isStringPelimdrome(data){
    while(start<end){
        if(data[start] !== data[end]){
            result = false;
        }
            start++;
            end--;
    }
    return result;
}
isStringPelimdrome(str)
console.log(result);

How many times does an element occur in an array?

let pets = ["dog", "cat", "goat", "dog", "cat"];
let obj={}
for (let i = 0; i < pets.length; i++) {
  if (!obj[pets[i]]) {
    obj[pets[i]] = 1;
  } else {
    obj[pets[i]]++;
  }
}

console.log(obj);

How to Display the Sum of Odd and Even Numbers in an Array?

let arr=[1,2,3,4,5,6,7,8,9,10,34,54,65,66,72,81,91,95,96,97]
let even = 0;
let odd = 0;

for(let i=0;i<arr.length;i++){
if(arr[i]%2==0){
    even +=arr[i]
}else{
    odd +=arr[i]
}
}
console.log('Odd', odd);
console.log('Even', even);

How to check String is an Anagram?

let str1 = 'hello';
let str2 = 'ohele'
let strObj = {}

function isStringAnagram(str1,str2){
if(str1.length == str2.length){
    for(i of str1){
        strObj[i] = (strObj[i] || 0)+1;
    }
    // console.log(strObj);

    for(i of str2){
        if(!strObj[i]){
            console.log('This is not Anagram CN2');
            return 'This is not Anagram CN2';
        } else{
            strObj[i]--
            // console.log(strObj);    
            }
    }
    console.log('this is Anagram');
}else{
    console.log('This is not Anagram CN1');
    return 'This is not Anagram CN1'
}
}
isStringAnagram(str1, str2);

How to check the Max-Occured character in the string?

let str = "Hello this is Bhavesh and you are watching DSA tutorial";
let obj = {};
let maxKey = "";

for (let i = 0; i < str.length; i++) {
    // console.log(str[i]);
    let key = str[i];
    if(key==' '){
        continue;
    }
    if(!obj[key]){
        obj[key]= 0
    }
    obj[key]++;
    if(maxKey == '' || obj[key]>obj[maxKey]){
maxKey=key;
    }
}
console.log(obj);
console.log(maxKey);

How to remove duplicates from an array?

let arr = [1, 3, 4, 6, 4, 6, 7, 3, 8, 3];
let newArr = []

for (let i = 0; i < arr.length; i++) {

    if(newArr.indexOf(arr[i])===-1){
        // newArr.push(arr[i])
        console.log(newArr.length, arr[i]);
        newArr.length = arr[i];
    }
}
console.log(newArr);

How to reverse an array using recursion?

let arr = [10,35,74,64,36,24,73]

function arrayReverse(array,start,end){
    console.log(arr);
if(start<=end){
    let temp = arr[start]
    arr[start] = arr[end];
    arr[end] = temp;
    arrayReverse(array, start+1, end-1);
}
}
arrayReverse(arr,0,arr.length-1)

How to reverse a string using stack?

let arr = [];
let str = "Bhavesh";
str = str.split("");
let len = arr.length;
let lastDigit = "";

function push(val) {
  arr[len] = val;
  len++;
}

function pop() {
  if (len > 0) {
    lastDigit = arr[len - 1];
    len = len - 1;
    arr.length = len;
    return lastDigit;
  } else {
    alert("Array is already Empty");
  }
}
function revString(data) {
  for (let i = 0; i < data.length; i++) {
    push(data[i]);
  }
  for (let i = 0; i < data.length; i++) {
    data[i] = pop();
  }
}

revString(str);
console.log(str);
str = str.join('')
console.log(str);

Searching Algorithms JavaScript DSA Questions

Here you will get some of the Searching Algorithms JavaScript DSA Questions for practice.

Write a program for Linear Search Algorithm.

let arr = [10,30,39,10,39,28,93,84,47,39,39,27,29,9,10,39,28,93,84,47];
let find = 39;
let position = []
let apearTimes = 0;

for(let i=0;i<arr.length;i++){
    if(find==arr[i]){
        position.push(i)
        apearTimes = position.length;
    }
}
console.log(position);
console.log(apearTimes); 

Write a program for the Binary Search Algorithm.

let arr=[1,2,3,4,5,6,7,8,9,10,34,54,65,66,72,81,91,95,96,97]
let find=2;
let start = 0;

let end = arr.length-1;
position = undefined;

while(start<=end){
    let mid = Math.floor((start + end)/2)
    if (arr[mid]==find) {
        position = mid;       
        break;
    }else if(arr[mid]<find){
        start = mid+1
    }else{
        end=mid-1;
    }

}
console.log(position);

Write a program for a recursive binary Search Algorithm.

let arr=[1,2,3,4,5,6,7,8,9,10,34,54,65,66,72,81,91,95,96,97]
let start = 0;
let end = arr.length-1;
let find = 65;
let position =undefined;

function recursiveBinarySearch(data,start,end){
    let mid = Math.floor((start+end)/2);
    if(data[mid]==find){
        position=mid;
        return true;
    } else if(data[mid]<find){
        recursiveBinarySearch(data,mid+1,end);
    }else{
        recursiveBinarySearch(data,start,mid-1)
    }
}
recursiveBinarySearch(arr,start,end)
console.log(position);

Sorting Algorithms JavaScript DSA Questions

Here you will get some of the Sorting Algorithms JavaScript DSA Questions for practice.

Write a code for the Bubble sort Algorithm.

let arr = [1, 3, 6, 33, 5, 88, 7, 4, 9, 11, 36, 94, 34, 99];
let temp;
for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr.length; j++) {
    if (arr[j] > arr[j + 1]) {
      temp = arr[j + 1];
      arr[j + 1] = arr[j];
      arr[j] = temp;
    }
  }
}

Write a code for the Insertion sort Algorithm.

let arr = [8, 4, 6, 2, 9];

function insertionSort(data) {
  let i, j, current;
  for (i = 1; i < data.length; i++) {
    current = data[i];
    j = i - 1;
    while (j >= 0 && data[j] > current) {
      data[j + 1] = data[j];
      j--;
    }
    data[j + 1] = current;
    console.log(arr);
  }
}
insertionSort(arr);
console.log(arr);

Write a code for the Selection sort Algorithm.

let Arr = [95, 3, 23, 36, 32, 64, 76, 45, 35];

function selectionSort(data) {
  for (let i = 0; i < data.length; i++) {
    let min = i;
    for (let j = i + 1; j < data.length; j++) {
      if (data[min] > data[j]) {
        min = j;
      }
    }
    let temp = data[i];
    data[i] = data[min];
    data[min] = temp;
  }
}

selectionSort(Arr);
console.log(Arr);

Coming Soon…

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top