Mid-Level JavaScript Interview

preview_player
Показать описание
I do a mock mid-level JS interview with an awesome developer Nikko Elliott.

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

Nice interview! and nice zigzag problem. While resolving it I managed to spot a small problem in the comments: the result for 4 rows is not correct (minute 3:07), the letter "S" is missing from the resulted string.

Thanks Justin Lawrence!

IliesMihaiAndrei
Автор

So i was doing the exercise on my own before seeing the result in the video and its funny how it came down to the same approach

this was my code:

const zigzagLetters = (str, num) => {
if (num === 1 || str.length <= num) return str
const rows = [...Array(num)].map( _ => [])

for (let i = 0, x = 0, up; i < str.length; i++) {
rows[x].push(str[i])
if (x === 0) up = true
if (x === num-1) up = false
up ? x++ : x--
}

return rows.flat(1).join('')
}

blue_keyboard
Автор

I challenged myself and tried to create a function as efficient as possible, for my current skill level. I tried not to use too many arrays with that in mind.

I am open to feedback on my answer :)

function diagonaliseString(str, rows) {
if (rows <= 1) return str;

const substringArray = str.split("");
if (substringArray[1] === undefined) return substringArray[0];

const substringLength = rows * 2 - 2;
let ans = [];

ans.push(substringArray[0]);

let currSubstring = substringLength;

while (currSubstring < substringArray.length) {

currSubstring += substringLength;
}

let left = 1;
let right = substringLength - 1;

while (left !== right) {
for (let j = 0; j < substringLength; j++) {
let nthSubstring = substringLength * j;
if (substringArray[left + nthSubstring] === undefined) continue;

if (substringArray[right + nthSubstring] === undefined) {
ans.push(substringArray[left + nthSubstring]);
continue;
}

ans.push(substringArray[left + nthSubstring]);
ans.push(substringArray[right + nthSubstring]);
}
left++;
right--;
}

for (let i = 0; i < str.length / substringLength; i++) {
ans.push(substringArray[left + substringLength * i]);
}

return ans.join("");
};

Great video by the way!

ShredNekM
Автор

First problem is so annoying, it is literally among most disliked problem

abhinaygupta