Tricky Coding Interview Question - JavaScript

preview_player
Показать описание
Solution to a very popular coding interview question asked recently by Amazon, Apple, Adobe - Plus One.

#softwareengineering #javascript #shorts
Рекомендации по теме
Комментарии
Автор

*the question you get asked when applying for a front end webdev job*

LaughingMan
Автор

I didn't know it was in Js. Eitherway I thought of this, int("".join([arr]))+1 then after realising we need the result in that list format, converted it to str and sliced into a list. So final solution

xClairy
Автор

I'm not sure if I'm a fool, but I would solve this by typecasting the elements into strings, concatenating and then typecasting that into an integer

And then adding 1, and turning it back into a list

fgvcosmic
Автор

I don’t think you’re handling the case where the length of the number increases… my approach would be recursion on the left n-1 digits until you hit something other than 9 as right most digit, or you end up with an empty array (in which case you return [1]).

Jake-mczk
Автор

[int(y) for y in list(str(int( "".join([str(x) f
or x in array])) + 1))]

roryb.bellows
Автор

im not as clever as you but this does work:
function incDigits(arr) {
let aux = [];
let str = '';
for (let i=0; i < arr.length; i++) {
str += arr[i].toString();
}
aux.push(str);
let addInt = parseInt(aux[0]);
addInt++;
aux[0] = addInt;
let stringy = aux[0].toString();
aux[0] = stringy;
let newString = aux[0];
let spl = newString.split("");
let intArr = [];
for (let i=0; i<spl.length; i++) {

}
return intArr;
}

johnsmith
Автор

I think this cannot handle [9] where it might return [0] by running the loop only once

vengateshvaidyanathang
Автор

Now do this in C where we have static arrays. Not so simple then 🤣

dough-pizza
Автор

I used your code and it failed for [1, 9] and [9, 9]. Did anyone else run into this problem?

siberiokhovonick
welcome to shbcf.ru