Hi Kevin, I have my Google, Microsoft and Amazon interviews lined up.I am practicing coding from leetcode kindly let me know if anything else I should be doing
Found a much easier and less complex way to do this.. I didn't understand your code. /** Write a Java program to add two binary numbers. */ import java.util.*; class Question171{ public static void main(String [] args){ Scanner s = new Scanner(System.in); System.out.println("Enter the 1st Binary Number"); String a = s.next(); System.out.println("Enter the 2nd Binary Number"); String b= s.next(); int x = Integer.parseInt(a,2); int y = Integer.parseInt(b,2); int z = x+y; System.out.println("Sum of Binary Numbers :"+Integer.toBinaryString(z)); } } See so simple
Hi Kevin, I have a Facebook interview coming up next week, taking up problems from your channel and leetcode. Please let me know if I could do anything apart from this.
Hi Kevin, I love to watch your videos daily with a cup of coffee, and afterwards trying myself in different problems. Please, keep doing all the tutorials as they are a great source of inspiration and motivation towards a better future. :)
I know this is an old video but I have watched just about every video I can find on this problem over the last few days trying to wrap my head around it and this is by far the clearest and easiest to follow of all of them. Thank you!
Hey Kevin, let me tell you, you are one of the best you tubers and instructors for these algorithm and data structures problems. Thank you for everything
Hi Kevin, shout out for your channel! I am currently preparing for the interview. Your content helps me a lot, if you would have any time to chat and advice me for the interview and learning process please let me know! Kuddos!
Definitely a good video! Maybe one suggestion would be to say / list a few problems that are solved similarly? I know that the array and Linked List version of this problem are also available and use almost the exact same method. Maybe one other to mention would be why use a StringBuilder (in python I have to emulate it with an array of chars, but some operations like insert at 0 would be expensive)
Hi Kevin, your videos are very helpful. They have clear explanation and go a long way in understanding the solution. A quick q: when you do an insert at 0th index in StringBuilder, won't it shift all characters by 1? If we have n characters and each time it shifts n characters by 1, won't it become O(n^2)?
Hey! Anytime I'm so happy to hear that you're enjoying the videos. You're totally right it's very possible that could be how insert works under the hood. With a normal string I would guess that would be the case with StringBuilder I would hope it'd be more efficient / smart but it might not be. If it does degrade the runtime it'd prob be best to build the string backwards and then reverse it at the end, what do you think?
Each iteration, you are shifting O(max(a,b)) chars to the right. It's max(a,b)^2 operation. Better to sb.reverse().toString() at the end, that's linear operation.
Kevin, although it works but explanation at time 6:50 for line#17 is not complete. We can have either (0, 1, 2 or 3 as well). We can have 3 if carry is 1 and we have both the bits as 1. So 1+1+1 = 3. Similarly for line#18, carry would be 1 for 2 cases (if sum is 2 or sum is 3, i.e 2/2=1 and 3/2 = 1). BDW, great and neat solution.
It is important to note that /2 also deals well with the case of sum == 3. That is, if the previous calculation forced a carry, and we are adding 1 and 1, we will have sum = 3, where (int)(3/2) = 1 and we get the right carry. Another way to do this, which might be more obvious to a code-reviewer, would be to use ` carry = sum > 1 ? 1 : 0; `
Hi Kevin ,your videos are really helpful ,I am from the QA background and luckily I got an interview lined up with FB for an Automation role ,I am expecting coding interview as my first round ,I am practicing from leetcode ,but this DSA is very new to me , please let me know if you can provide some suggestion on the same.
Thanks so much! I do but I don't feel that it's my place to share honestly. What I can tell you is that several people have reached out to me telling me they have received offers from placing like Facebook and that my channel has helped them which is awesome to hear!
Great walkthrough ! So is the time complexity O(max(m,n)), m,n is the length of string a,b; space complexity is also O(max(m,n)), which is the size of sb object ?
Hey David! I'd recommend checking out my Patreon page for some of the services I offer to help people prep: www.patreon.com/KevinNaughtonJr. I also do mock interviews if you're interested in that (I'm currently helping 2 other people prep for FB interviews as well!) If you're' interested or have any questions/want to chat reach out to me on twitter: twitter.com/KevinNaughtonJr
Thanks Kevin , very helpful , I have an interview with FB this week , can you please give me some references to find the latest problems asking into the interview . Thanks in advance.
Hey Arup, sorry for the late response! I hope the interview went well if you already had it! If not, I offer help like that in my Discord server. If you're interested in joining check out my Patreon page! www.patreon.com/KevinNaughtonJr
Hey Kevin, Thank you very much for the problem. But as a side note. Instead of inserting each element at the beginning (because that involves shifting items to the right), we could reverse "sb" (sb.reverse()) at the end in O(1) that gives us faster run time... Anyway great solution.
Hey Chetan, Thank you very much for the side note, but you're probably wrong. Faster runtime is the way was on the video. You can check it yourself by simply subtracting the time difference before and after for two approaches.
@@turskyi Lol, it's already been an year? It takes O(N*N) time complexity to insert something on index 0 each time, if you append at the end, and reverse it. It takes only O(N) time complexity
i just cant understand the case where our carry is 1, i-th element is also 1, and j-th element is also 1, if anyone can explain me that case, it would be really helpful.. :) great tutorial btw.
Shain Dholakiya hey Shain that’s awesome! I have some tiers to help people prepare for their interviews on my Patreon page if you’re interested in joining one! Here’s the link: www.patreon.com/KevinNaughtonJr
@@suyogsubedi8584 don't worry you'll get the job as well. However never stop like I did or it's hard if you want to switch... You won't find time to do this unfortunately
Explanation at 5:36 on this video for below code does not look correct to me. sum+=a.charAt(i--)-'0'; Its not 0 minus 0 will yield to Zero thing. Actually each character has a corresponding decimal value. In Java when we are assigning a character to int, it will just assign ascii decimal value. To offset we subtract ascii decimal value. For 0: int p = s1.charAt(0);// Assigns value as 48 int q = s1.charAt(0)-'0';//Assigns 0 as (48-48)=0 For 1. int p = s1.charAt(1);// Assigns value as 49 int q = s1.charAt(1)-'0';//Assigns 1as (49-48)=1 check this table: www.rapidtables.com/code/text/ascii-table.html So: CharacterDecimal 048 149 250 351 452 553 654 755 856 957
Anurag Mishra thanks for the detailed explanation! I guess I didn't go into as much depth as you but that's what I meant by 0 minus 0. I was referring to the offset that would be generated by the ascii values
@@KevinNaughtonJr You should have gone into depth about that thing though cause in this whole problem, that line only was the most confusing thing of this whole solution.
Guys I've cheated in this one, but if you wanna try something else than the video, then here you go :D. class Solution: def addBinary(self, a: str, b: str) -> str: aDec = int(a,2) bDec = int(b,2) sum = aDec + bDec output = str(bin(sum))[2:] return output
Have a Facebook phone interview for SWE in about 1.5 weeks. Should I just grind LC? On LC's Interview Discussion forum, a lot of people got straight up LC questions. Would love to know more. You can also contact me at justnealpatel.com/ so we can get in touch via email.
Hey Neal thanks for reaching out! I would def recommend LeetCode for prep. If you wanna discuss more feel free to join the Discord channel: www.patreon.com/KevinNaughtonJr
I think it probably depends on the level you're at honestly...but if you can buy both that's probably best. If you can only pick one, I'd probably choose EPI
@@KevinNaughtonJr Gotcha. I actually already have both but you'd put EPI ahead of CTCI in terms of prep? I mainly just want to use CTCI for the process of going through tougher problems, I believe it's the BUD system and the tips on behavioral stuff sounds important. For the problems, EPI seems to offer the more challenging problems.
Let me know how I can help you guys!!! Also if there's anything else you guys would want to see on this channel, leave it in the comments!
Hey, I have a facebook interview coming up and would love to connect/get your feedback!
Hi Kevin, I have my Google, Microsoft and Amazon interviews lined up.I am practicing coding from leetcode kindly let me know if anything else I should be doing
Found a much easier and less complex way to do this.. I didn't understand your code.
/** Write a Java program to add two binary numbers. */
import java.util.*;
class Question171{
public static void main(String [] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter the 1st Binary Number");
String a = s.next();
System.out.println("Enter the 2nd Binary Number");
String b= s.next();
int x = Integer.parseInt(a,2);
int y = Integer.parseInt(b,2);
int z = x+y;
System.out.println("Sum of Binary Numbers :"+Integer.toBinaryString(z));
}
}
See so simple
Hi Kevin, I have a Facebook interview coming up next week, taking up problems from your channel and leetcode. Please let me know if I could do anything apart from this.
Hey, I have a Google interview coming up and would love to connect!
Hi Kevin, I love to watch your videos daily with a cup of coffee, and afterwards trying myself in different problems. Please, keep doing all the tutorials as they are a great source of inspiration and motivation towards a better future. :)
Hey Pawel I'm so happy to hear that. Thank you so much for the kind words they mean the world to me!!!
I know this is an old video but I have watched just about every video I can find on this problem over the last few days trying to wrap my head around it and this is by far the clearest and easiest to follow of all of them. Thank you!
happy i was able to help, thanks for watching :')
You are awesome!!! After following you for one month, I got to write the code exactly the style as yours. I am sooo happy haha.
Hey Kevin, let me tell you, you are one of the best you tubers and instructors for these algorithm and data structures problems. Thank you for everything
Love your videos. Great help. Its fascinating to see you solving problems.
Thanks Hamza!
You explained the concept very well Sir.Love to see your Coding Videos.
Hi Kevin, shout out for your channel! I am currently preparing for the interview. Your content helps me a lot, if you would have any time to chat and advice me for the interview and learning process please let me know! Kuddos!
this is awesome buddy, I wrote almost 130 lines of code to solve this easy (after your explanation) problem...good job.
Very nice and helpful! Thanks.
How would you recommend to improve my problem solving skills? I really think I am bad at it.
Definitely a good video! Maybe one suggestion would be to say / list a few problems that are solved similarly? I know that the array and Linked List version of this problem are also available and use almost the exact same method. Maybe one other to mention would be why use a StringBuilder (in python I have to emulate it with an array of chars, but some operations like insert at 0 would be expensive)
Good idea! Thanks for the advice Martin!!!
Hi Kevin, your videos are very helpful. They have clear explanation and go a long way in understanding the solution. A quick q: when you do an insert at 0th index in StringBuilder, won't it shift all characters by 1? If we have n characters and each time it shifts n characters by 1, won't it become O(n^2)?
Hey! Anytime I'm so happy to hear that you're enjoying the videos. You're totally right it's very possible that could be how insert works under the hood. With a normal string I would guess that would be the case with StringBuilder I would hope it'd be more efficient / smart but it might not be. If it does degrade the runtime it'd prob be best to build the string backwards and then reverse it at the end, what do you think?
We can convert binary strings to its relevant integer and then do addition. After addition we can convert from integer to binary string.
I have a doubt, the while condition will exit if any one of the string terminates ,then won't we have to add the remaining char to our final ans?
Each iteration, you are shifting O(max(a,b)) chars to the right. It's max(a,b)^2 operation. Better to sb.reverse().toString() at the end, that's linear operation.
yes you r right
Kevin, although it works but explanation at time 6:50 for line#17 is not complete. We can have either (0, 1, 2 or 3 as well). We can have 3 if carry is 1 and we have both the bits as 1. So 1+1+1 = 3. Similarly for line#18, carry would be 1 for 2 cases (if sum is 2 or sum is 3, i.e 2/2=1 and 3/2 = 1). BDW, great and neat solution.
Hey, just what I needed bro :) gonna try this problem before I watch the vid! Love the intro too btw haha
Haha thanks buddy, make sure to message me after and let me know how the studying is going!!! Good luck!!!
thanks man, planning on messaging u soon with some progress :)
About to answer you!
You can solve this by implementing a half adder circuit. Sum bit is a XOR b, carry bit is a AND b.
Facebook has been known to ask this but without using the + operator. You would have to use bit manipulation in that case
It is important to note that /2 also deals well with the case of sum == 3. That is, if the previous calculation forced a carry, and we are adding 1 and 1, we will have sum = 3, where (int)(3/2) = 1 and we get the right carry.
Another way to do this, which might be more obvious to a code-reviewer, would be to use ` carry = sum > 1 ? 1 : 0; `
Hi Kevin ,your videos are really helpful ,I am from the QA background and luckily I got an interview lined up with FB for an Automation role ,I am expecting coding interview as my first round ,I am practicing from leetcode ,but this DSA is very new to me , please let me know if you can provide some suggestion on the same.
this was super helpful !
You are awesome bro.... thanks for all these videos...
Hi kevin, do u have any suggestion how to.prepare for behaviour interview on facebook? I have one in few days. Thanks
Hey great material. I subscribed! Do you know if the folks you talked to got their respective positions?
Thanks so much! I do but I don't feel that it's my place to share honestly. What I can tell you is that several people have reached out to me telling me they have received offers from placing like Facebook and that my channel has helped them which is awesome to hear!
@Kevin - am currently prepping for tech interview. Can you give me some areas to focus on? Can we talk? Thank you!
This is my 5th day, and I try my best to come up with solutions. But still end up finding solutions online. Is this common or am I not cut off?
Great walkthrough ! So is the time complexity O(max(m,n)), m,n is the length of string a,b; space complexity is also O(max(m,n)), which is the size of sb object ?
Thanks and yes that sound about right to me :)
Time complexity is actually n^2 ,as u r shifting entire string to right in each iteration
Hey man I have a Facebook interview coming up and would love some advise.
Binary addition is the same as we do in case of decimal nos.
Hey man I have a mock interview with facebook next week and the real one a week after that. Im pretty nervous. Do you have some advice?
Hey David! I'd recommend checking out my Patreon page for some of the services I offer to help people prep: www.patreon.com/KevinNaughtonJr. I also do mock interviews if you're interested in that (I'm currently helping 2 other people prep for FB interviews as well!) If you're' interested or have any questions/want to chat reach out to me on twitter: twitter.com/KevinNaughtonJr
Thanks Kevin , very helpful , I have an interview with FB this week , can you please give me some references to find the latest problems asking into the interview . Thanks in advance.
Hey Arup, sorry for the late response! I hope the interview went well if you already had it! If not, I offer help like that in my Discord server. If you're interested in joining check out my Patreon page! www.patreon.com/KevinNaughtonJr
Very good brother.
Hi, Kevin I have an interview with Google on Monday. I want to talk to you and need your guidance
I didn't understand the sum mod 2 part what happens when a[i]=1,b[j]=1 and carry=1 then sum=3 So sum mod 3 should come right
is that a helicopter going by at 3:12?
sb. insert(0, c) is an O(N) operation. should we use sb. append(c) and then reverse it?
right
thanks you much!
Hey Kevin, Thank you very much for the problem. But as a side note. Instead of inserting each element at the beginning (because that involves shifting items to the right), we could reverse "sb" (sb.reverse()) at the end in O(1) that gives us faster run time... Anyway great solution.
Hey Chetan, Thank you very much for the side note, but you're probably wrong.
Faster runtime is the way was on the video. You can check it yourself by simply subtracting the time difference before and after for two approaches.
@@turskyi Lol, it's already been an year? It takes O(N*N) time complexity to insert something on index 0 each time, if you append at the end, and reverse it. It takes only O(N) time complexity
i just cant understand the case where our carry is 1, i-th element is also 1, and j-th element is also 1, if anyone can explain me that case, it would be really helpful.. :) great tutorial btw.
Thank you for the video :) I’ve a Facebook interview coming up, would love to talk to you
Same here! Check out my Patreon page for the tier that’d be the most helpful for you! www.patreon.com/KevinNaughtonJr
thank you Kevin! :D
Anytime I hope it was helpful! :)
Hi Kevin, I have Facebook interview coming up and would love to get your feedback.
Love your vids man! I have a Facebook internship interview coming up. Would love to connect and talk
Shain Dholakiya hey Shain that’s awesome! I have some tiers to help people prepare for their interviews on my Patreon page if you’re interested in joining one! Here’s the link: www.patreon.com/KevinNaughtonJr
@@KevinNaughtonJr Joined!
how do you guys come up with the %2 and /2 thing I would have never thought of that. :(
after 1 year have you started to think like this?
@@suyogsubedi8584 no way. maybe because I got a job and didn't bother practicing everyday. I was doing leetcode everyday when I made that comment.
@@shashanksetty8110 ayy you got the job that is what matters. Currently in the same phase you were a year ago.
@@suyogsubedi8584 don't worry you'll get the job as well. However never stop like I did or it's hard if you want to switch... You won't find time to do this unfortunately
@@shashanksetty8110 thank you for the advice brother
This comment has nothing to do with coding. Kevin, the way you look at her is everything. makes me wanna have that myself. love both of you ❤❤
When are you going to marry?
Whats the time complexity for that program?
Asking the right questions :D
@@varunram5413 lol
I have an interview with one of FAANGs, can you please help?
Sandip Bhaumik definitely! Check out my Patreon page and sign up for whatever will help you the most: www.patreon.com/KevinNaughtonJr
Explanation at 5:36 on this video for below code does not look correct to me.
sum+=a.charAt(i--)-'0';
Its not 0 minus 0 will yield to Zero thing.
Actually each character has a corresponding decimal value.
In Java when we are assigning a character to int, it will just assign ascii decimal value. To offset we subtract ascii decimal value.
For 0:
int p = s1.charAt(0);// Assigns value as 48
int q = s1.charAt(0)-'0';//Assigns 0 as (48-48)=0
For 1.
int p = s1.charAt(1);// Assigns value as 49
int q = s1.charAt(1)-'0';//Assigns 1as (49-48)=1
check this table:
www.rapidtables.com/code/text/ascii-table.html
So:
CharacterDecimal
048
149
250
351
452
553
654
755
856
957
Anurag Mishra thanks for the detailed explanation! I guess I didn't go into as much depth as you but that's what I meant by 0 minus 0. I was referring to the offset that would be generated by the ascii values
thank you @@KevinNaughtonJr
@@KevinNaughtonJr You should have gone into depth about that thing though cause in this whole problem, that line only was the most confusing thing of this whole solution.
Incase, a.length < 10 && b.length < 10 then solution is as below:
public String addBinary(String a, String b) {
return Integer.toBinaryString(Integer.parseInt(a, 2) + Integer.parseInt(b, 2));
}
Incase, a.length < 19 && b.length < 19
public String addBinary(String a, String b) {
return Long.toBinaryString(Long.parseLong(a, 2) + Long.parseLong(b, 2));
}
big O? thank you!!
I was asked this last week by Fb :)
you got the job bro?
@@unknownman1 cleared the tele rounds, waiting for on-site interview :)
Method to convert characters to integers
it will fail for case 0 and 0
I tested my code against "0" and "0" and it passes :)
Awesome video.. Can you please help me
Hey I'm happy to help!!! Shoot me an email :)
Your email Id
Guys I've cheated in this one, but if you wanna try something else than the video, then here you go :D.
class Solution:
def addBinary(self, a: str, b: str) -> str:
aDec = int(a,2)
bDec = int(b,2)
sum = aDec + bDec
output = str(bin(sum))[2:]
return output
from 2020.12
Please help I need a serious help in solving problems :D
Check out my Patreon page! Might be a tier that you can join that would be helpful for you :)
Have a Facebook phone interview for SWE in about 1.5 weeks. Should I just grind LC? On LC's Interview Discussion forum, a lot of people got straight up LC questions. Would love to know more. You can also contact me at justnealpatel.com/ so we can get in touch via email.
Hey Neal thanks for reaching out! I would def recommend LeetCode for prep. If you wanna discuss more feel free to join the Discord channel: www.patreon.com/KevinNaughtonJr
@@KevinNaughtonJr would you suggest EPI or CTCI to accompany it?
I think it probably depends on the level you're at honestly...but if you can buy both that's probably best. If you can only pick one, I'd probably choose EPI
@@KevinNaughtonJr Gotcha. I actually already have both but you'd put EPI ahead of CTCI in terms of prep? I mainly just want to use CTCI for the process of going through tougher problems, I believe it's the BUD system and the tips on behavioral stuff sounds important. For the problems, EPI seems to offer the more challenging problems.