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 [])
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-- }
I know we have plenty of CPU power and memory these days. Still it's a limited resource, and especially on mobile devices inefficient code will lead to hotter devices and lower battery life. And yes, JavaScript engines these days contain a lot of magic to keep the code running fast, which makes it hard to optimize for, because you never can be 100% sure how you code is getting interpreted, optimized and run. And it's even harder to tell what the GC does, how much waste your code produces, etc. But still there are some fundamentals that apply to every code: Copying memory and immediatly throwing it away is wasted (de)allocation of memory. And also branching can make the code run much slower, if the CPU mispredicts a jump. Maybe the JavaScript JIT compiler will take care of it, maybe it doesn't, depending on how/when/how often your function is called, and which concrete JS runtime is executing the code. So it's best to just avoid those things: There are two unnecessary array copies before the loop, just use Array.from({ length: num}, () => []), which will allocate and initialize the whole array in a single operation. And then there's lot's of unnecessary branching inside the loop, you could just have used something like x += (x === num-1 && x !== 0) ? -1 : 1, or even get completely rid of the branching by replacing the ternary operator using some math tricks: x += ~(x === num-1 && x !== 0)*2+3. No branching === no potential false branch prediction :) Especially inside a loop, you want your code to be efficient, because you know it won't be executed only once.
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!
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
I have an interview in a few days, and this is gold. The last part about asking clarifying questions is so good.
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 [])
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('')
}
I know we have plenty of CPU power and memory these days. Still it's a limited resource, and especially on mobile devices inefficient code will lead to hotter devices and lower battery life.
And yes, JavaScript engines these days contain a lot of magic to keep the code running fast, which makes it hard to optimize for, because you never can be 100% sure how you code is getting interpreted, optimized and run. And it's even harder to tell what the GC does, how much waste your code produces, etc.
But still there are some fundamentals that apply to every code: Copying memory and immediatly throwing it away is wasted (de)allocation of memory. And also branching can make the code run much slower, if the CPU mispredicts a jump. Maybe the JavaScript JIT compiler will take care of it, maybe it doesn't, depending on how/when/how often your function is called, and which concrete JS runtime is executing the code.
So it's best to just avoid those things:
There are two unnecessary array copies before the loop, just use Array.from({ length: num}, () => []), which will allocate and initialize the whole array in a single operation.
And then there's lot's of unnecessary branching inside the loop, you could just have used something like x += (x === num-1 && x !== 0) ? -1 : 1, or even get completely rid of the branching by replacing the ternary operator using some math tricks: x += ~(x === num-1 && x !== 0)*2+3. No branching === no potential false branch prediction :)
Especially inside a loop, you want your code to be efficient, because you know it won't be executed only once.
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!
Great catch!
Zigzag problem is not nice
love this content justin
First problem is so annoying, it is literally among most disliked problem
glad I am not the only one raging about it, while trying to solve it...
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
Good videos