Published: 21 Dec 2019 › Updated: 21 Dec 2019![JavaScript interview questions [with solutions]](https://i.ecency.com/p/C3TZR1g81UNaPs7vzNXHueW5ZM76DSHWEY7onmfLxcK2iPPkhwAocu4Wb8wRioGgtEKbGQQh9FpceL8HyD5JoS7DnjBecwDiJidJgY9Hzd1sY1hiQWwwFre?format=match&mode=fit&height=377)
JavaScript interview questions [with solutions]
I have been a software engineer for almost 5 years now but (for one reason or another) only today for the first time I interviewed another developer for a position in my company.
Here is the simple exercise that I asked the (junior) candidate to solve:
Write a function that given 2 arrays of characters returns
whether the 2 arrays contain an acronym.
Eg.
[ 'b', 'i', 'n', 'a', 'r', 'y' ]
[ 'b', 'r', 'a', 'i', 'n', 'y' ]
=> TRUE
[ 'b', 'o', 'a', 't' ]
[ 't', 'a', 'b', 'o', 'o' ]
=> FALSE
Here are a couple of simple solutions:
Solution 1:
var isAcronym = () => {
if (!arr1.length || arr1.length != arr2.length) return false;
arr1.forEach((el) => {
const id = arr2.indexOf(el);
if (id == -1) return false;
arr2[id] = null;
});
return arr2.filter(el => !!el).length === 0;
}
var arr1 = ['b','a','g','r','e','i','e','l'];
var arr2 = ['g','a','b','r','i','e','l','e'];
console.log('Is Acronym: ', isAcronym(arr1, arr2));
- Complexity: O(N)
- Optimization: length check, best case: O(1)
- Spatial complexity: in loco
Solution 2:
var isAcronym = () => {
if (!arr1.length || arr1.length != arr2.length) return false;
const count = {};
arr1.forEach(el => {
if (count[el]) count[el] += 1;
else count[el] = 1;
});
arr2.forEach(el => {
count[el] -= 1;
});
return Object.values(count).filter(res => res !== 0).length === 0;
};
var arr1 = ['g','a','b','i','r','e','l','e'];
var arr2 = ['g','a','b','r','i','e','l','e'];
console.log('Is Acronym: ', isAcronym(arr1, arr2));
- Complexity: O(N)
- Optimization: length check, best case: O(1)
- Spatial complexity: O(N)
Did I miss any bugs? =)
Leave JavaScript interview questions [with solutions] to:
Read more #stem posts
Best Posts From Gabe
We have not curated any of gaottantacinque's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Gabe
- Crypto Shots
- Swarm of Swarms #58 - Hive Development Updates for Week of October 20th, 2025
- Hive/INLEO Weekly Recap — Crypto Shots Rewards (Aug 11–17, 2025)
- Every beginning is difficult
- Swarm of Swarms #44 - Hive Development Updates for Week of July 14th, 2025
- Ani ❤️ CRYPTO SHOTS
- Play CRYPTO SHOTS
- Swarm of Swarms #31 - Hive Development Updates for Week of April 14th, 2025
- Weekly stream on MazerHQ Twitch
- Annual Recap
- HAPPY NEW YEAR
- VOTE #1 CRYTO SHOTS BEST SHOOTER
- Layer 1 NFT on Hive for gaming marketplace now open (Launch Day)
- MAZER GAMING PvP Tournament - 2nd Edition
- MAZER GAMING PvP Tournament - 2nd Edition
- JavaScript: fetch token price, consume custom_json data on Hive
- New Sponsors for our Weekly PVP Tournaments
- Feeling the BITCOIN ETF FOMO? Play in our Tournaments and keep stacking sats! - SUNDAY = FREE 2 EARN
- WIN $HBD & NFTs on ConsumerBreak! FOUNDER INTRO + NFTOPIA Metaverse Convention (first post)
- The Hive blockchain rocks! crimsonclad on stage @ The NFTopia