kennymkchan / interview-questions-in-javascript Goto Github PK
View Code? Open in Web Editor NEWA mostly reasonable collection of technical software development interview questions solved in Javascript
License: MIT License
A mostly reasonable collection of technical software development interview questions solved in Javascript
License: MIT License
It might be easier for people to understand the implementation of the examples if they can run the code and play around with it. It would also be nice to have tests for each example.
I can help out with this if you think it might be helpful 😺
Find below optimized code for intersection of two arrays:
var firstArray = [2, 2, 4, 1,5,2,3,6,4,22,2];
var secondArray = [1, 2, 0, 2];
intersection(firstArray, secondArray); // [2, 1]
function intersection(firstArray, secondArray) {
let finalArray=[];
for(let i=0;i<firstArray.length;i++){
if(secondArray.indexOf(firstArray[i])> -1){
finalArray.push(firstArray[i]);
}
}
console.log([... new Set(finalArray)]);
}
array = [7,4,2,9,9,15,1]
The answer doesn't handle if array[i] === array[i-1]
or in the case of array above, 9,9
5.1 is a classic solution in most languages that have real integer types. Unfortunately, JS number is not a real integer. It will fall back to 32-bit signed integer when doing bitwise operations.
Just simply try this:
isPowerOfTwo(2 ** 43 - 2 ** 40)
// true
(2 ** 43 - 2 ** 40).toString(16)
// "70000000000"
function findMissingNumber(arrayOfIntegers, upperBound, lowerBound) {
// Iterate through array to find the sum of the numbers
var sumOfIntegers = 0;
for (var i = 0; i < arrayOfIntegers.length; i++) {
return sumOfIntegers += arrayOfIntegers[i]; <---- this is the missing return keyword
}
// Find theoretical sum of the consecutive numbers using a variation of Gauss Sum.
// Formula: [(N * (N + 1)) / 2] - [(M * (M - 1)) / 2];
// N is the upper bound and M is the lower bound
upperLimitSum = (upperBound * (upperBound + 1)) / 2;
lowerLimitSum = (lowerBound * (lowerBound - 1)) / 2;
theoreticalSum = upperLimitSum - lowerLimitSum;
return theoreticalSum - sumOfIntegers;
}
saös
a palindrome? Certainly not! However, after deleting the non-latin letter, it suddenly is, causing a false positive.replace(/[^A-Za-zäöüÄÖÜßłŧøþæſðđŋħĸłΩŁŦ¥ıØÞÆẞÐŊĦŁ]+/g,"")
or something like that, here's a better idea: first convert to lowercase, then filter out all whitespace. And just in case you get the idea to write .toLowerCase().replace(/[ \t\r\n]/g,"")
(which obviously leaves out the less common whitespace things like zwnbsp or half-space), use the character group instead of trying to reinvent a broken wheel. In short, the following works: .toLowerCase().replace(/\s/g, "")
(At least it works much better.)(
is encountered, and decrease if a )
is encountered. If at any point in time the counter becomes negative, it's invalid. If the counter is non-zero in the end, it's invalid. In all other cases, it's valid.decimalToBinary(0)
returns "0"
And finally:
Hi. Looking at https://github.com/kennymkchan/interview-questions-in-javascript#array--product, a comemnt says // greatest product is either (min1 * min2 * max1 || max1 * max2 * max3)
Surely the greatest product is the one with from max1 * max2 * max3, where does min1 * min2 * max1 come from? Or am I missing something?
function computeProduct(raw_data) {
return raw_data
.sort((a, b) => Math.abs(a) - Math.abs(b))
.slice(-3)
.reduce((accumulator, num) => accumulator * num);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.