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...
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));
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.
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
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!
So glad you enjoyed it!
Extremely helpful to have a human explain through the often confusingHackerRank interface.
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!
Yeah agreed! Thanks so much for watching :)
Yes it's modeling confidence despite initial uncertainty. This is the goal.
This is absolutely great. Could you do more JavaScript interview prep videos?
That's the plan :)
I've turned on notifications for all videos
@@adelaykay Yay!
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?")
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
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.
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!
@@JamesQQuick Theme name please.
@@shakeebarsalan7030 purple
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 :)
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
Really enjoyed watching this, I think its great that you talk through options for one problem. Keep em coming !!
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.
Love it. Thanks for sharing :)
Regex is great, it takes a little bit to understand but is worth the effort to learn.
*ᴡᴀᴛ'sᴀᴘ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ, ᴛᴇʟʟ ʜɪᴍ ɪ ʀᴇғғᴇʀʀᴇᴅ ʏᴏᴜ ᴛᴏ ʜɪᴍ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ.. 🖤💯☝️
Please do more of these!
Tkank for this video could you make a serie of this, is very interesting. Regards from México
**ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆
What is the difference between instance and object??🤔
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.
Thank you, I have an interview in 30 minutes. wish me luck!
Good luck! Let me know how it goes!!
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. 😉
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.
@@JamesQQuick got it thanks!!
Thanks
Did I miss an instruction not to use RegEx to split the string? const tokens = s.split(/[ !.,_'@?]+/) in JavaScript.
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.
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.
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!
@@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.
awesome
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
May i know the theme name you are using for Syntax colors?
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
Great video, lot's of learning here.
**ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆
Convert string to array use the destructuring operator..... const stringArray = [...stringVariable]
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
Hackerman James
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?
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.
+1 to this. Great question though Jess Daniel. I'm glad you called out that I should have re-tested!
Seriously? No string.match()?
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)
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?")
"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.
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.
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')
@@lionelt87 Yeah I goofed, should have been .replaceAll() (I think that would work) or a regex.
@@jasonrm999 thanks for the replaceAll mention, i nearly forgot it exists!
**ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆
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));
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!
@@JamesQQuick Of course! Thank you for the video! Your channel has taught me a lot and helped me through several projects recently.
**ɢᴇᴛ ʜᴇʟᴘ ɪᴍᴍᴇᴅɪᴀᴛᴇʟʏ ɴᴏᴡ ʙʏ ʀᴇᴀᴄʜɪɴɢ
ᴏᴜᴛ ᴛᴏ ᴍᴀx ᴛʜᴏᴍᴀs ᴅɪʀᴇᴄᴛʟʏ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ⬆️🆕.
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))
**ᴄᴏɴᴛᴀᴄᴛ ᴍᴀx ᴛʜᴏᴍᴀs, ᴛᴇʟʟ ʜɪᴍ ᴛʜᴀᴛ ɪ ʀᴇғᴇʀʀᴇᴅ ʏᴏᴜ ғᴏʀ sᴏᴍᴇᴛʜɪɴɢ ɴᴇᴡ ᴘʀᴏғɪᴛᴀʙʟᴇ ʜᴜɢᴇ. ⬆️✍️👆