JavaScript coding interview question

These are few coding interview question which I faced during my interview journey with different companies. I hope you will find helpful it.

1. Write a program for anagram.

An anagram is a word or phrase formed by rearranging the letters in another word or phrase.

let s1 = 'spar'
let s2 = 'rasp'

function isAnagram(s1, s2) {
    if (s1.length !== s2.length) return false;
    for (let c of s1) {
        if (s2.indexOf(c) === -1) return false
    }
    return true;
}

2. Write a function which will accept a string of characters and find the count of each character.

let str = 'hello everyone';

function count(str) {
    let coutObj = {}
    for(let c of str) {
        coutObj[c] = coutObj[c] ? coutObj[c] + 1 : 1
    }
    return coutObj;
}

console.log(count(str))

3. De-structuring of given object.

let user = {
    name: "Anil",
    age: 32,
    fullname: {
        first: 'Anil',
        last: 'kumar'
    }
}

const { name, age } = user
const { fullname: { first, last } } = user;
OR
const { fullname: { first, last }, name, age } = user

4. Write a program to print n prime number

function getPrimeNumber(n) {
    if(n == 0) return [];
    if (n == 1) return [1];
    let primeArray = [1, 2];
    let isPrime = true;
    for (let i = 3; i <= n; i++) {
        for (let j = i - 1; j < i && j > 1; j--) {
            if (i % j === 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) primeArray.push(i)
        isPrime = true;
    }
    return primeArray;
}

console.log(getPrimeNumber(100));

5. Write a program: how many step you will take to check str2 first char can match with str1 first char

let str1 = 'cabcd';
let str2 = 'bddcc';
function check(str1, str2) {
    let firstChar = str1[0];
    if (str2.indexOf(firstChar) === -1) return -1;
    if (firstChar === str2[0]) return 0;
    //let tempStr = '';
    let steps = 0;
    for (let c of str2) {
        if (c === firstChar) {
            steps = steps + 1;
            //tempStr += c;
            break;
        } else {
            steps = steps + 1;
            //tempStr += c;
        }
    }
    return { steps};
}
console.log(check(str1, str2));

6. Write a program to sort an object for a give value.

//  sort on the basis of name
const items = [
    { name: 'Edward', value: 21 },
    { name: 'Sharpe', value: 37 },
    { name: 'And', value: 45 },
    { name: 'The', value: -12 },
    { name: 'Magnetic', value: 13 },
    { name: 'Zeros', value: 37 }
];

let res = items.sort(function (a, b) {
    // return a.value - b.value; // sorting with value
    return a.name.localeCompare(b.name);
    //    return (a, b) => a.value.localeCompare(b.value, "en", { numeric: true });
});

console.log(res);

7. Write a program sum(1)(2)(3)…. Or Implement infinite currying

// Infinite currying sum(1)(2)(3)....
function sum1(a) {
    return function(b) {
        if(b) return sum1(a+b);
        return a;
    }
}

console.log(sum1(1)(3)(4)(6)())

8. Write a program to reverse a string using recursion.

function strR(str) {
    // base
    if(!str.length) return '';
    if(str.length === 1) return str;
    return strR(str.substr(1, str.length)) + strR(str[0]);
}

console.log(strR('anil'));

9. Write a program to flat an array (without using inbuilt method)

function flatDeep(arr, d = 1) {
    return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), []) : arr.slice();
};

let nestedArray = [1, [2, 3], [1, 3, [5, [4]]]];
console.log(flatDeep(nestedArray, Infinity));

10. Write a function which accept nXm dimension array and give result like this

ex1 => [[1,2,3],[4,5,6]] => [[5,7,9]] ex2=> [[2,3],[5,6]] => [[7,9]]

function arrayReduce(arr) {
    output = [];
    arr.reduce((a, b) => {
        console.log({a,b})
        let temp = []
        for(let [index, val] of a.entries()) {
            //console.log({index, val})
            temp[index] = val+ b[index];
        }
        output.push(temp);
        return temp;
    });
    return output;
}

console.log(arrayReduce([[1,2,3],[4,5,6]]));

Leave a Reply