Forgot to add a break statement if you find the time you generated to not be valid (i.e. there's no point in checking the rest of the digits if you have already found one that invalidates the current time) so if you guys want to marginally speed up your algorithm go for it. Or if you wanna roast me for forgetting that's cool too I guess your choice
i think in an interview setting, merely explaining how you could optimize your algorithm is sufficient. that invalidation is a little more complicated than a 1 or 2 minute change
Great approach! But I think there's a simpler approach- we can do just increase the minute by 1. If the minute reaches 60, we increase the hour If the hour reaches 24, we just reset the day. I found my approach quite simple as well. Would love your feedback!
you could write stringToTime and timeToString helper functions. both are 1-3 lines. they'll return an integer representing number of minutes from 0 to that time. so the answer ends up as follows: const END_OF_DAY = stringToTime('23:59'); const startTime = stringToTime(time); const digits = new Set([time[0], time[1], time[3], time[4]]); for (let i = startTime + 1; i < END_OF_DAY; i++) { //loop until end of day const cur = timeToString(i); if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) { return cur; } } for (let i = 0; i < startTime; i++) { //start over at time 0 const cur = timeToString(i); if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) { return cur; } }
Clean approach! I'm an ex-G engineer, so hope you don't mind feedback. The upper bound to your solution will take 60*24=1440 iterations (in the case of 00:00). One observation is that there are only 4*4*4*4=256 possible time strings, and we can even prune out invalid strings such as 44:90. A more efficient solution would be to recursively generate possible strings and pull out the correct one.
I agree that there are 2 methods - check all possible times and see if the digit combination is valid / check all possible permutations and minimize the difference. I think in the interview I'd probably go with kevin's approach because it's SO MUCH SIMPLER TO CODE and you don't have to write the recursive permutations function. however, given 45-60 minutes you could probably do either. but so much of a coding interview is about communication and understanding the needs of the company versus your capabilities. so if you exhaust all of your time with the coding problem, you won't be able to learn much about the actual role. This, I think, is a glaring problem with most software interview schedules. So much time is spent on problem solving as opposed to direct discussions to see if both parties' career goals can be met.
@@JM_utube agree with last part. There should be discussion on fit. Not everyone can code well under pressure but I get it that knowing algorithms and data structures will make on a better programmer.
Can anyone solve this in 30 min if they have never seen this problem before? I was totally off track and never thought about this solution because it seems too brute force, but I couldn't come up with anything better.
Ugh math-ish problems are the worst, but dealing with time-related code seems like an important thing in the industry! Thankfully, there are just enough memes in this video to distract me from the fact that I dislike math-ish problems and allows me to focus on the problem itself 😂 Thanks for the upload Kevin and keep up the great work!
Suggestion :In general, it would be helpful if you can explain the problem and your approach over the white board at the beginning and then start with coding. That would make much more sense than just directly jumping into coding!
Hey Suhas, I do my best to explain while also keeping the video short and hopefully amusing. I think some people will get bored if I take too long to explain something so i do my best to explain quickly or explain as i go!
yes please keep the videos short and to the point like this. I have seen so many other long boring videos where the person tried to use a white board and I just wanted them to move to the coding part but they never did or did so in a hurried way
Just curious but for anyone who has gone through an interview, with this being one of Google's favorite questions, is this something that could be given during a first round interview in the mix of the 2-3 questions to solve in 45 minutes? Or would this be more of an isolated type of higher round systems design type question where you get the whole time to break this problem down? I'm trying to gauge the amount of time I give myself (on my timer) to solve these types of questions vs the Fibonacci type questions.
Hey Harsh yeah you're right, I've been trying to be better about that I just forget to sometimes. I'd say this solution's stime complexity is O(N) where N is the number of possible times we can generate and I think the space complexity is constant
@@KevinNaughtonJr I'd think this would be a constant time algorithm as N is constant here, you can walk thru all possible times in a constant amount of time (24*60 iterations)
So we are dealing in military time from 00:00 to 23:59. Suppose clock is at 23:59, adding next minute would give it 00:00 instead 24:00 So to keep the time in this range(00:00 to 23:59), we need to mod our time value by total minutes in 24 hrs i.e 24*60 to keep minutes in the range. So whenever we are at 23:59 and clock strikes next minute, we should achieve 00:00. Hope this helps!!
Wow! What an approach! Made it so easy! Thanks kevin! I was banging my head making it so complicated , it is funny, i was actually trying like grabbing the last digit adding 1 to it and checking if it is under minutes limit and so on..totally dumb and faulty approach..lol. Thanks a lot!
Hello Kevin, love your videos, quite helpful, Thank you so much. One odd question: what was that music track with Piano in the background at the beginning of this video? Couldn't find it myself
The "minutes = (minutes + 1) % (24 x 60)" line will keep the total minutes from exceeding 1439, which will bind the time to stay within 00:00 to 23:59 inclusive. So for that example, 23:59 will keep iterating from 00:00 onward until it hits 22:22. Remember the problem is asking for the *next* closest time, which doesn't include the closest time before.
Anytime Tali! This is just simulating a clock and adding a minute every time, but I guess you could of also taken the valid digits and made permutations of those digits and then checked if the time you have is greater than your starting time?
Google does not care about this solution. What it cares about is developing thinking and solving it by second approach, that is by using only the given 4 digits. This is what all matters in a Google interview. Next time if you want to solve a Google question, then don't do the naive one. Instead, do the one which requires some thinking.
Forgot to add a break statement if you find the time you generated to not be valid (i.e. there's no point in checking the rest of the digits if you have already found one that invalidates the current time) so if you guys want to marginally speed up your algorithm go for it. Or if you wanna roast me for forgetting that's cool too I guess your choice
i think in an interview setting, merely explaining how you could optimize your algorithm is sufficient. that invalidation is a little more complicated than a 1 or 2 minute change
I like your videos. It would be great if you could explain time and space complexities for your solutions.
Will not roast chill, got the problem explanation.
Great approach!
But I think there's a simpler approach- we can do just increase the minute by 1.
If the minute reaches 60, we increase the hour
If the hour reaches 24, we just reset the day.
I found my approach quite simple as well. Would love your feedback!
Aman Bhardwaj I agree
you could write stringToTime and timeToString helper functions. both are 1-3 lines. they'll return an integer representing number of minutes from 0 to that time. so the answer ends up as follows:
const END_OF_DAY = stringToTime('23:59');
const startTime = stringToTime(time);
const digits = new Set([time[0], time[1], time[3], time[4]]);
for (let i = startTime + 1; i < END_OF_DAY; i++) { //loop until end of day
const cur = timeToString(i);
if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) {
return cur;
}
}
for (let i = 0; i < startTime; i++) { //start over at time 0
const cur = timeToString(i);
if (digits.has(cur[0]) && digits.has(cur[1]) && digits.has(cur[3]) && digits.has(cur[4])) {
return cur;
}
}
Clean approach! I'm an ex-G engineer, so hope you don't mind feedback.
The upper bound to your solution will take 60*24=1440 iterations (in the case of 00:00). One observation is that there are only 4*4*4*4=256 possible time strings, and we can even prune out invalid strings such as 44:90. A more efficient solution would be to recursively generate possible strings and pull out the correct one.
Sounds interesting.
Would you be kind enough to give ur recursive solution here? I can't seem to wrap my head around it.
I was thinking about the same.Here provided solution is brute force.
I agree that there are 2 methods - check all possible times and see if the digit combination is valid / check all possible permutations and minimize the difference. I think in the interview I'd probably go with kevin's approach because it's SO MUCH SIMPLER TO CODE and you don't have to write the recursive permutations function. however, given 45-60 minutes you could probably do either. but so much of a coding interview is about communication and understanding the needs of the company versus your capabilities. so if you exhaust all of your time with the coding problem, you won't be able to learn much about the actual role. This, I think, is a glaring problem with most software interview schedules. So much time is spent on problem solving as opposed to direct discussions to see if both parties' career goals can be met.
@@JM_utube agree with last part. There should be discussion on fit. Not everyone can code well under pressure but I get it that knowing algorithms and data structures will make on a better programmer.
Can anyone solve this in 30 min if they have never seen this problem before? I was totally off track and never thought about this solution because it seems too brute force, but I couldn't come up with anything better.
Ugh math-ish problems are the worst, but dealing with time-related code seems like an important thing in the industry! Thankfully, there are just enough memes in this video to distract me from the fact that I dislike math-ish problems and allows me to focus on the problem itself 😂
Thanks for the upload Kevin and keep up the great work!
Thanks Dom! I do my best to keep the videos short, entertaining, and comical (if possible) :)
Love your approach. Thanks for sharing!
Hi Kevin,
Kindly explain 489. Robot Room Cleaner leetcode problem.
For beginner PFB better solution:
public static String nextClosestTime(String time) {
HashSet set = new HashSet();
set.add(new Integer(time.charAt(0)- '0'));
set.add(new Integer(time.charAt(1)- '0'));
set.add(new Integer(time.charAt(3)- '0'));
set.add(new Integer(time.charAt(4)- '0'));
int hours = Integer.parseInt(time.substring(0,2));
int mins = Integer.parseInt(time.substring(3));
while(true) {
mins++;
hours = (hours + mins/60) % 24;
mins = mins % 60;
if(set.contains(hours/10) &&
set.contains(hours%10) &&
set.contains(mins/10) &&
set.contains(mins%10)) break;
}
return String.format("%02d", hours) + ":" + String.format("%02d", mins);
}
Thanks Buddy, you made it look like an easy problem.
Suggestion :In general, it would be helpful if you can explain the problem and your approach over the white board at the beginning and then start with coding. That would make much more sense than just directly jumping into coding!
For me I prefer these short videos as they don’t tend to be drawn out and pedantic
Hey Suhas, I do my best to explain while also keeping the video short and hopefully amusing. I think some people will get bored if I take too long to explain something so i do my best to explain quickly or explain as i go!
@@asphix Yeah I try my best to keep them short and to the point because I worry that people will get bored if the explanations are too drawn out
yes please keep the videos short and to the point like this. I have seen so many other long boring videos where the person tried to use a white board and I just wanted them to move to the coding part but they never did or did so in a hurried way
Just curious but for anyone who has gone through an interview, with this being one of Google's favorite questions, is this something that could be given during a first round interview in the mix of the 2-3 questions to solve in 45 minutes? Or would this be more of an isolated type of higher round systems design type question where you get the whole time to break this problem down? I'm trying to gauge the amount of time I give myself (on my timer) to solve these types of questions vs the Fibonacci type questions.
I get a Time Limit Exceeded Error for this approach
It'll help if you talk about the time and space complexity of your algorithms too.
Hey Harsh yeah you're right, I've been trying to be better about that I just forget to sometimes. I'd say this solution's stime complexity is O(N) where N is the number of possible times we can generate and I think the space complexity is constant
@@KevinNaughtonJr I'd think this would be a constant time algorithm as N is constant here, you can walk thru all possible times in a constant amount of time (24*60 iterations)
You are soooo amazing ! I have my Google interview tonight ..I hope all these helps !
Hey, I have my interview in 1 week, could you please share how many questions were asked? and what was the topic?
Did you get the job?
@@MIDNightPT4 no ! Rejected
@@aryansaxenafanclub3184 hopefully urs went well !!
I am not able to understand this line
minute = (minute + 1 ) % ( 24 * 60)
Someone please help.
same! I'm also stuck here :(
So we are dealing in military time from 00:00 to 23:59. Suppose clock is at 23:59, adding next minute would give it 00:00 instead 24:00
So to keep the time in this range(00:00 to 23:59), we need to mod our time value by total minutes in 24 hrs i.e 24*60 to keep minutes in the range. So whenever we are at 23:59 and clock strikes next minute, we should achieve 00:00.
Hope this helps!!
Great explanation, thank you!
Wow! What an approach! Made it so easy! Thanks kevin! I was banging my head making it so complicated , it is funny, i was actually trying like grabbing the last digit adding 1 to it and checking if it is under minutes limit and so on..totally dumb and faulty approach..lol. Thanks a lot!
Explains all problem efficiently then no issue. Misses one break and everyone loses their mind. XD
kevin can you explain and solve leetcode problem no.1067 digit count in range
Hello Kevin, love your videos, quite helpful, Thank you so much. One odd question: what was that music track with Piano in the background at the beginning of this video? Couldn't find it myself
hope you can publish more video about leetcode solutions
Hi Kevin,
Can you explain how this code will satisfy
Input: "23:59"
Output: "22:22"
This is for the next day
The "minutes = (minutes + 1) % (24 x 60)" line will keep the total minutes from exceeding 1439, which will bind the time to stay within 00:00 to 23:59 inclusive. So for that example, 23:59 will keep iterating from 00:00 onward until it hits 22:22. Remember the problem is asking for the *next* closest time, which doesn't include the closest time before.
@@fatcat22able Thanks my friend !
heyyy can you please discuss the optimal BST problem
What's the problem number? I'll put it on my list :)
Thanks for the video! What's the motivation for your brute force solution vs. a solution that only iterates through the relevant digits?
Anytime Tali! This is just simulating a clock and adding a minute every time, but I guess you could of also taken the valid digits and made permutations of those digits and then checked if the time you have is greater than your starting time?
Can you please make a video on longest palindromic substring. Leetcode 5?
Thanks for the suggestion I'll see what I can do!
You forgot to put break when isValid=False found
pause the video at exactly 4:50 ,look at kevin's face , thank me later
Riju Ban LOVE IT
Thank you making all the problems seem so easy. If its ok would you please do The Median of Two sorted Array (Leetcode No 4) .Thanks Again :)
Anytime!!! I'll see what I can do :)
Google does not care about this solution. What it cares about is developing thinking and solving it by second approach, that is by using only the given 4 digits. This is what all matters in a Google interview.
Next time if you want to solve a Google question, then don't do the naive one. Instead, do the one which requires some thinking.
#review