Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views4 pages

JS Coding Questions CompanyWise

The document contains JavaScript coding interview questions categorized by company, including Google, Amazon, Microsoft, Meta, and Infosys/TCS/Wipro. Each section provides various coding challenges such as reversing words in a sentence, implementing debounce functions, and finding unique elements in arrays. The document includes code snippets and example outputs for each question to demonstrate the expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

JS Coding Questions CompanyWise

The document contains JavaScript coding interview questions categorized by company, including Google, Amazon, Microsoft, Meta, and Infosys/TCS/Wipro. Each section provides various coding challenges such as reversing words in a sentence, implementing debounce functions, and finding unique elements in arrays. The document includes code snippets and example outputs for each question to demonstrate the expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JavaScript Interview Coding Questions

(Company-Wise)
Google

Reverse words in a sentence


function reverseWords(str) {
return str.split(" ").reverse().join(" ");
}
console.log(reverseWords("I love JavaScript")); // "JavaScript love I"

Implement debounce function


function debounce(func, delay) {
let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
}

Deep clone an object


function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
console.log(deepClone({a:1, b:{c:2}}));

Find unique elements in two arrays


function uniqueElements(arr1, arr2) {
return [...new Set([...arr1, ...arr2])];
}
console.log(uniqueElements([1,2,3],[3,4,5])); // [1,2,3,4,5]

Implement Promise.all polyfill


Promise.myAll = function(promises) {
return new Promise((resolve, reject) => {
let results = [], completed = 0;
promises.forEach((p, i) => {
Promise.resolve(p).then(val => {
results[i] = val;
completed++;
if(completed === promises.length) resolve(results);
}).catch(reject);
});
});
};

Amazon

First non-repeating character in string


function firstUniqueChar(str) {
let count = {};
for (let char of str) count[char] = (count[char]||0)+1;
for (let char of str) if (count[char] === 1) return char;
return null;
}
console.log(firstUniqueChar("swiss")); // "w"

Rotate array by k steps


function rotate(arr, k) {
k = k % arr.length;
return arr.slice(-k).concat(arr.slice(0, -k));
}
console.log(rotate([1,2,3,4,5], 2)); // [4,5,1,2,3]

Longest substring without repeating characters


function lengthOfLongestSubstring(s) {
let set = new Set(), left = 0, maxLen = 0;
for(let right=0; right<s.length; right++){
while(set.has(s[right])) set.delete(s[left++]);
set.add(s[right]);
maxLen = Math.max(maxLen, right-left+1);
}
return maxLen;
}
console.log(lengthOfLongestSubstring("abcabcbb")); // 3

Microsoft

Implement setTimeout using setInterval


function mySetTimeout(callback, delay) {
let interval = setInterval(() => {
callback();
clearInterval(interval);
}, delay);
}
mySetTimeout(()=>console.log("Executed"),1000);
Merge two sorted arrays
function mergeSorted(arr1, arr2) {
let res = [], i=0, j=0;
while(i<arr1.length && j<arr2.length){
if(arr1[i]<arr2[j]) res.push(arr1[i++]);
else res.push(arr2[j++]);
}
return res.concat(arr1.slice(i)).concat(arr2.slice(j));
}
console.log(mergeSorted([1,3,5],[2,4,6]));

Meta (Facebook)

Currying function implementation


function curry(fn) {
return function curried(...args) {
if(args.length >= fn.length) return fn.apply(this, args);
return (...next) => curried.apply(this, args.concat(next));
};
}
function add(a,b,c){return a+b+c;}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // 6

Memoization function
function memoize(fn) {
let cache = {};
return function(x) {
if(x in cache) return cache[x];
return cache[x] = fn(x);
};
}
const square = memoize(x=>x*x);
console.log(square(5)); // 25

Infosys/TCS/Wipro

Factorial of a number
function factorial(n) {
if(n===0) return 1;
return n*factorial(n-1);
}
console.log(factorial(5)); // 120
Check Palindrome
function isPalindrome(str) {
return str === str.split("").reverse().join("");
}
console.log(isPalindrome("madam")); // true

You might also like