Facebook JavaScript Interview Question | Learn Web Development Now

preview_player
Показать описание
Try to solve the question yourself first!

Learn Web Development NOW!
HTML, CSS, & JavaScript Tutorials

❌ No reproduction without prior authorization!
Рекомендации по теме
Комментарии
Автор

Awesome explanation! I've got my interview with FB coming up so this definitely helps!

chris
Автор

the video's time complexity is O(n^2) where n is the longer of (a, b). This is because the while loop is n time complexity, and for each loop we do slice/substring/split, which are all n time complexity. You can avoid that extra time complexity by avoiding those expensive operations:

const sum = (a, b) => {
let index = 1;
let carryover = 0;
let result = [];

while (index <= Math.max(a.length, b.length) || carryover) {
const first = a[a.length - index] || 0;
const second = b[b.length - index] || 0;

const localSum = Number(first) + Number(second) + carryover;
carryover = Math.floor(localSum / 10);
result.push(localSum % 10);

index += 1;
}

return result.reverse().join('');
}

This runs in O(n) time complexity, which is as good as we can do. Both solutions require O(n) space to store the result.

stantoncbradley
Автор

Did you discuss about time complexity of this solution? Were they satisfied? I’m getting prepared for interview and using slice splice or other looping methods inside loops concerning me, its not easy to come up with optimal runtime quick when you have eyes on you

maksatbekburkanov
Автор

This solution is great for integers, but not for decimals.

guohaolay
Автор

function sum(a, b) {
if (!a) return b;
if (!b) return a;
const n = +a[a.length - 1];
const m = +b[b.length - 1];

let nm = n + m;

if (nm > 10) {
b = (+b + 10).toString();
nm -= 10;
}

return sum(a.substring(0, a.length - 1), b.substring(0, b.length - 1)) + nm;
}

wasfi
Автор

Did you accept FB offer? What did you find is the best resource to prepare for Facebook Front end interview

smaranh
Автор

What about this one? : const summ = (a, b) => (BigInt(a) + BigInt(b)).toString()

imgubish
Автор

function sum(a, b){
let A = a*1 //multiply by 1 auto converts to a number
let B = b*1
let O = A+B; //you would want to add some additional checks, etc.
console.log('output is ' + O);
}

sum(2, 2.5); //num num
sum('2', 2.5); //str num
sum(2, '2.5'); //num str
sum('2', '2.5'); //str str
//str str

NickBrawley
Автор

i would assume/hope this is the type of material people usually know after 2+ years of coding? ive been coding a year but only about 2-3 hours a day. while this isnt COMPLETELY lost on me its def not the level im at. would people say this is junior level stuff? seems abit more advanced. good video tho!

BobbyBundlez
Автор

can anyone explain to me what " (+carrying) " means? i don't recognize that syntax

maurosouto
Автор

What if one of the number is negative ?

hariharankm
Автор

With no substring / slice /split /reverse() op involved

function sum(a, b) {
let totalSum = '';
let carryOver = false;
let index = 0;

while (index <= a.length - 1 || index <= b.length - 1) {

let tempSum = '';

if (index <= a.length - 1) {
tempSum = Number(a[a.length - 1 - index]) + (carryOver ? 1 : 0);
}

if (index <= b.length - 1) {
tempSum = Number(b[b.length - 1 - index]) + tempSum;
}

tempSum = tempSum.toString();
totalSum = tempSum[tempSum.length - 1] + totalSum;
carryOver = Number(tempSum) > 10;

index++;
}
return (carryOver ? '1' : '') + totalSum;

}

arunaveerappan