Mid-Level JavaScript Interview

Поделиться
HTML-код
  • Опубликовано: 9 ноя 2024

Комментарии • 13

  • @Nemesisthetrickster
    @Nemesisthetrickster Месяц назад

    I have an interview in a few days, and this is gold. The last part about asking clarifying questions is so good.

  • @blue_keyboard
    @blue_keyboard Год назад +5

    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('')
    }

    • @benm1295
      @benm1295 Год назад

      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.

  • @IliesMihaiAndrei
    @IliesMihaiAndrei Год назад +1

    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!

  • @JSDEVD
    @JSDEVD Год назад +1

    love this content justin

  • @abhinaygupta4052
    @abhinaygupta4052 Год назад +3

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

    • @Gabryhoo
      @Gabryhoo Год назад

      glad I am not the only one raging about it, while trying to solve it...

  • @ShredNekM80
    @ShredNekM80 Год назад

    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

  • @harpo187bling
    @harpo187bling Год назад +2

    Good videos