JavaScript Interview Practice with HackerRank - Strings, Big O Notation, and more!

Поділитися
Вставка
  • Опубліковано 7 вер 2024
  • Let's get better at JavaScript whiteboard interview questions by solving a HackerRank problem live. I'll talk through my strategy, how I communicate during these problems, and how I talk through optimizing a problem taking into account performance.
    The Challenge - hackerrank.com...
    STAY IN TOUCH 👋
    Check out the Podcast - compress.fm
    Newsletter 🗞 - www.jamesqquic...
    Live streams on Twitch 🖥️ - / jamesqquick
    Follow me on Twitter 🐦 - / jamesqquick
    QUESTIONS...?
    Join the Discord Server 💬 - / discord
    Want to know what hardware and software I use? www.jamesqquic...
    Learn Web Development 📖 - www.jamesqquic...

КОМЕНТАРІ • 79

  • @DraxyBearWD
    @DraxyBearWD Рік тому +10

    My solution:
    const sample = "He is a very very good boy, ins't he?";
    const tokenise = (str) => {
    let output = str.split(/[\s!,?._'@]/).filter((w) => w.length > 0);
    output.unshift(output.length);
    return output.join("
    ");
    };
    console.log(tokenise(sample));

  • @komald7048
    @komald7048 2 роки тому +11

    This is a great video, breaking down the thought process on solving these problems. I like the way you went from for loops to more modern using .map() I am looking forward to more.

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @surayamelina
    @surayamelina 10 місяців тому +1

    So glad I found your channel! This is exactly the kind of video I've been looking for. Great to see an example of what you'd do in a real interview setting and how you talk through all your questions/uncertainties. In my interview prep that's what I've been really curious about, what does it actually look like to solve a problem in real time - glad I'm not the only one asking myself a lot of questions and changing my mind as I go!

    • @JamesQQuick
      @JamesQQuick  10 місяців тому

      So glad you enjoyed it!

  • @RetroMan4321
    @RetroMan4321 9 місяців тому

    Extremely helpful to have a human explain through the often confusingHackerRank interface.

  • @francisngo
    @francisngo Рік тому +1

    I think what is so valuable about this video is that you still expressed confidence in figuring out the problem even when you felt unsure about how something work (e.g. regex). I think how I would approach is talking through the entire problem before coding while I am walking through the problem. Regardless, this video demonstrated break downs of thought process which I found valuable. Kudos!

    • @JamesQQuick
      @JamesQQuick  Рік тому

      Yeah agreed! Thanks so much for watching :)

    • @RetroMan4321
      @RetroMan4321 9 місяців тому

      Yes it's modeling confidence despite initial uncertainty. This is the goal.

  • @JohnDoeX1966
    @JohnDoeX1966 2 роки тому +10

    This is absolutely great. Could you do more JavaScript interview prep videos?

    • @JamesQQuick
      @JamesQQuick  2 роки тому +1

      That's the plan :)

    • @adelaykay
      @adelaykay 2 роки тому +1

      I've turned on notifications for all videos

    • @JamesQQuick
      @JamesQQuick  2 роки тому +1

      @@adelaykay Yay!

  • @avertry9529
    @avertry9529 2 роки тому +5

    I'll post my answer before watching. Harder than it looks.
    function res(string) {
    const split = string.split(/[\W\d]/g)
    console.log(split.length.toString())
    split.map((r) => r != '' && console.log(r))
    }
    res("How are you today, isn't it a lovely day, better than the last 3 days?")

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @wadecodez
    @wadecodez 2 роки тому +10

    Personally I would have used a single loop and pushed items to an array of arrays. Each time a special character occurs I would start a new array. It would be slightly more efficient because split, join, and filter all imply another loop. Good video though.

    • @JamesQQuick
      @JamesQQuick  2 роки тому +2

      Nice! Thanks for sharing your approach. The good thing is that a finite number of loops (const value) is relatively negligible in terms of Big O, but keeping everything in one loop is a nice tough!

    • @shakeebarsalan7030
      @shakeebarsalan7030 Рік тому

      @@JamesQQuick Theme name please.

    • @xynyde0
      @xynyde0 Рік тому +1

      @@shakeebarsalan7030 purple

  • @jakubwikacz4558
    @jakubwikacz4558 2 роки тому +4

    Hi James, I have skipped the video this time after few minutes. Consider learning regexp as that is a good tool which you can use everywhere (frontend, backend, sql, searching in text files, converting texts). When you master regexps then you will have another tool in your hands, which will save time :) I do press a like button for a raw approach and a courage to share it as a video :)

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @shayasanova6272
    @shayasanova6272 Рік тому

    Really enjoyed watching this, I think its great that you talk through options for one problem. Keep em coming !!

  • @ConnecticutAngler
    @ConnecticutAngler 2 роки тому +1

    Two things I personally would’ve done differently. One is that I tend to use backticks to encompass strings when it’s convenient to avoid issues with literal strings containing single or double quotes; circumvents annoying, cumbersome-looking escapes. The second is that I probably would’ve tried replacing the special characters by doing a forEach loop on the special characters array and running a replaceAll on the source string for each. Great vid! These sorts of tests put developers on the spot, which can be uncomfortable, for sure. But I think it’s great to show the process and how you personally work through it.

    • @JamesQQuick
      @JamesQQuick  2 роки тому

      Love it. Thanks for sharing :)

  • @MC---
    @MC--- 2 роки тому +1

    Regex is great, it takes a little bit to understand but is worth the effort to learn.

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      *ᴡᴀᴛ'sᴀᴘ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ, ᴛᴇʟʟ ʜɪᴍ ɪ ʀᴇғғᴇʀʀᴇᴅ ʏᴏᴜ ᴛᴏ ʜɪᴍ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ.. 🖤💯☝️

  • @applemaysl
    @applemaysl Рік тому +1

    Please do more of these!

  • @ianespinosa2685
    @ianespinosa2685 2 роки тому +1

    Tkank for this video could you make a serie of this, is very interesting. Regards from México

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆

  • @paraschauhan9978
    @paraschauhan9978 2 роки тому +3

    What is the difference between instance and object??🤔

    • @JamesQQuick
      @JamesQQuick  2 роки тому +3

      Well, an instance is typically associated with Object Oriented Programming, where you have an instance of a class. The class is the definition for what an object looks like and the instance is the actual object itself. An object in JavaScript is basically anything that is not a primitive value.

  • @nicolasguillenc
    @nicolasguillenc Рік тому

    Thank you, I have an interview in 30 minutes. wish me luck!

    • @JamesQQuick
      @JamesQQuick  Рік тому

      Good luck! Let me know how it goes!!

  • @christopheanfry2425
    @christopheanfry2425 2 роки тому +1

    Very instructive video. I think you did it well on your first try but you just forgot to add blank space in your special character variable. Maybe I’m wrong i don’t have the experience you have. 😉

    • @JamesQQuick
      @JamesQQuick  2 роки тому +3

      Well, I don't want to consider a blank space as a special character since it becomes the delimeter when I call .split() at the end.

    • @christopheanfry2425
      @christopheanfry2425 2 роки тому

      @@JamesQQuick got it thanks!!

  • @tamantaman
    @tamantaman 2 роки тому +1

    Thanks

  • @stevenvallarsa1765
    @stevenvallarsa1765 2 роки тому +6

    Did I miss an instruction not to use RegEx to split the string? const tokens = s.split(/[ !.,_'@?]+/) in JavaScript.

    • @MC---
      @MC--- 2 роки тому +2

      This is the optimal way of doing it. When watching the video I was not 100% sure split could take a RegEx but thought it was likely.
      I think the global flag is required for cases where there are multiple instances of the special characters.

    • @Tomapon
      @Tomapon 2 роки тому +4

      It's fine to use regex but many people don't know it perfectly without looking it up (like in an interview situation as he mentioned).
      I think something like .filter(n => n) needs to be added to your solution in order to remove the last empty item in the array?
      If I only do s.split(/[ !.,_'@?]+/) the array has an empty item in the end, but if I do const tokens = s.split(/[ !.,_'@?]+/).filter(n => n) it seems fine.

    • @JamesQQuick
      @JamesQQuick  2 роки тому +2

      Yeah my take is that many people can't write regex from scratch/memory which is true for me. I thought it would be valuable to show people how I would handle the situation knowing that I couldn't come up with regex on the spot. Hopefully the thought process and being able to adapt is beneficial. I agree though, regex would be a great fit!

    • @gaborkovacs189
      @gaborkovacs189 2 роки тому

      @@Tomapon I think your regex is ok, but filter the whole array is unnecessary, because empty element can occur only at the beginning and at the end.

  • @BrentHarris00
    @BrentHarris00 2 роки тому +1

    awesome

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @jsayubi
    @jsayubi 2 роки тому +1

    May i know the theme name you are using for Syntax colors?

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @eduardom800
    @eduardom800 2 роки тому +1

    Great video, lot's of learning here.

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆

  • @suelingsusu1339
    @suelingsusu1339 2 роки тому +1

    Convert string to array use the destructuring operator..... const stringArray = [...stringVariable]

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @ming3957
    @ming3957 2 роки тому +1

    Hackerman James

  • @jessdaniel5881
    @jessdaniel5881 2 роки тому

    My only comment is you didn’t verify that the solution was still correct after using the Set approach. I was waiting for you to show the full output at the end because “very” appears twice in the input so did that still come out correctly?

    • @kyleskincoug
      @kyleskincoug 2 роки тому +2

      The set was only to contain the special characters for speeding up comparing the token characters to those. The output was still just array so duplicates should still be present.

    • @JamesQQuick
      @JamesQQuick  2 роки тому

      +1 to this. Great question though Jess Daniel. I'm glad you called out that I should have re-tested!

  • @DavidBcc
    @DavidBcc Рік тому

    Seriously? No string.match()?

  • @braveitor
    @braveitor 2 роки тому +1

    Another approach:
    const specialChars = new Set(['!', ' ',',', '?', '.', '_', '\'', '@'])
    const printTokens = (str, specialChars) => {
    let tempArray = [];
    let tempString = '';
    for (let i = 0; i < str.length; i++) {
    const letter = str[i];
    if (!specialChars.has(letter)) {
    tempString += letter
    } else if (letter!='') {
    tempArray.push(tempString)
    tempString = '';
    }
    }
    return(tempArray.filter(t=>t));
    }
    let filtered = printTokens("He is a very good boy, isn't he?", specialChars)
    console.log(filtered)

  • @omergatenio6802
    @omergatenio6802 Рік тому

    you can use the ASCII values when checking the charecters like this:
    function printTokens(s) {
    const tokens = [];
    let token = '';
    s.split('').forEach(char => {
    if ((char >= 'a' && char = 'A' && char console.log(token));
    }
    printTokens("He is a very very good boy, isn't he?")

  • @peteriannuzzo1525
    @peteriannuzzo1525 4 місяці тому

    "He is a very very good boy, isn't he?".replace(/^[^A-Za-z]+/, '').replace(/[^A-Za-z]+$/, '').split(/[^A-Za-z]+/g)
    .replace(s) will remove all unwanted characters at the beginning and end of string prior to splitting string.

  • @jasonrm999
    @jasonrm999 2 роки тому +2

    const solution = str => {
    const strArr = str
    .replace(/[!,?._'@]/g, ' ')
    .replace(' ', ' ')
    .trim()
    .split(' ');
    console.log(strArr.length);
    strArr.forEach(str => console.log(`${str}
    `));
    };
    solution(`He is a very very good boy, isn't he?`);
    Probably missing something (newbie), but it worked with examples I tested with.

    • @lionelt87
      @lionelt87 2 роки тому +1

      the .replace() call only replaces the first occurence of a string, so only the first double space gets replaced to a single one. only workaround is to use regex with 'g' (global) flag : 'aa aa aa'.replace(/aa/g, 'bb')

    • @jasonrm999
      @jasonrm999 2 роки тому +1

      @@lionelt87 Yeah I goofed, should have been .replaceAll() (I think that would work) or a regex.

    • @lionelt87
      @lionelt87 2 роки тому

      @@jasonrm999 thanks for the replaceAll mention, i nearly forgot it exists!

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆

  • @andreh.9300
    @andreh.9300 2 роки тому +1

    const inputString = "He is a very very good boy, isn't he?";
    function splitTokens(inputString) {
    let tokens = inputString.split(/[!,.@'?\s]+/);
    return tokens.filter(token => token !== "");
    }
    function printTokens(tokenArr) {
    console.log(tokenArr.length);
    for (let token of tokenArr) {
    console.log(token);
    }
    }
    printTokens(splitTokens(inputString));

    • @JamesQQuick
      @JamesQQuick  2 роки тому +1

      Yeah it would help if I could write Regex from scratch which I can't, and I don't think many people can honestly. I think its valuable to show people how you can still solve a problem intelligently in this type of scenario if the regex isn't accessible. That's a great solution though!

    • @andreh.9300
      @andreh.9300 2 роки тому

      @@JamesQQuick Of course! Thank you for the video! Your channel has taught me a lot and helped me through several projects recently.

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
      ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.

  • @ulutiu
    @ulutiu 2 роки тому +2

    const str = "He is a very very good boy, isn't he?"
    const tokens = str.split(/[^A-Za-z]/).filter(x => x != "")
    console.log(tokens.length)
    tokens.forEach(x => console.log(x))

    • @user-df7gr8no7q
      @user-df7gr8no7q 2 роки тому

      **ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆