Add Binary

Sdílet
Vložit
  • čas přidán 5. 07. 2024
  • For business inquiries email partnerships@k2.codes Discord: bit.ly/K2-discord
  • Věda a technologie

Komentáře • 114

  • @KevinNaughtonJr
    @KevinNaughtonJr  Před 5 lety +11

    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!

    • @sca3482
      @sca3482 Před 5 lety

      Hey, I have a facebook interview coming up and would love to connect/get your feedback!

    • @poojaraychaudhuri6306
      @poojaraychaudhuri6306 Před 5 lety

      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

    • @adam1996t
      @adam1996t Před 4 lety

      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

    • @Aadhya-Aadith
      @Aadhya-Aadith Před 4 lety

      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.

    • @nikitasurya9034
      @nikitasurya9034 Před 3 lety

      Hey, I have a Google interview coming up and would love to connect!

  • @pyxelr
    @pyxelr Před 5 lety +35

    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. :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +4

      Hey Pawel I'm so happy to hear that. Thank you so much for the kind words they mean the world to me!!!

  • @MrFrawsty
    @MrFrawsty Před 11 měsíci

    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!

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 11 měsíci

      happy i was able to help, thanks for watching :')

  • @littlesunshine6851
    @littlesunshine6851 Před 4 lety +1

    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.

  • @ericzorn3735
    @ericzorn3735 Před 4 lety

    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

  • @ahamza007
    @ahamza007 Před 5 lety +2

    Love your videos. Great help. Its fascinating to see you solving problems.

  • @RiteshKumar-qb6uz
    @RiteshKumar-qb6uz Před 4 lety

    You explained the concept very well Sir.Love to see your Coding Videos.

  • @olaz93
    @olaz93 Před 3 lety

    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!

  • @lapujain
    @lapujain Před 3 lety

    this is awesome buddy, I wrote almost 130 lines of code to solve this easy (after your explanation) problem...good job.

  • @orkhanhuseyn
    @orkhanhuseyn Před 4 lety

    Very nice and helpful! Thanks.
    How would you recommend to improve my problem solving skills? I really think I am bad at it.

  • @martinszauer4414
    @martinszauer4414 Před 5 lety

    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)

  • @badal1985
    @badal1985 Před 5 lety

    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)?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +5

      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?

  • @jaimitgandhi6330
    @jaimitgandhi6330 Před 4 lety

    We can convert binary strings to its relevant integer and then do addition. After addition we can convert from integer to binary string.

  • @nakuldeshmukh7901
    @nakuldeshmukh7901 Před rokem

    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?

  • @my3m
    @my3m Před 4 lety +3

    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.

  • @shahnawazalam9939
    @shahnawazalam9939 Před 2 lety

    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.

  • @TheNickcharlie
    @TheNickcharlie Před 5 lety +2

    Hey, just what I needed bro :) gonna try this problem before I watch the vid! Love the intro too btw haha

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +1

      Haha thanks buddy, make sure to message me after and let me know how the studying is going!!! Good luck!!!

    • @TheNickcharlie
      @TheNickcharlie Před 5 lety

      thanks man, planning on messaging u soon with some progress :)

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety

      About to answer you!

  • @chinmoykanjilal
    @chinmoykanjilal Před 4 lety +1

    You can solve this by implementing a half adder circuit. Sum bit is a XOR b, carry bit is a AND b.

  • @varunrao2135
    @varunrao2135 Před 4 lety

    Facebook has been known to ask this but without using the + operator. You would have to use bit manipulation in that case

  • @nickdrouin7142
    @nickdrouin7142 Před 3 lety +4

    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; `

  • @kemaroach
    @kemaroach Před 2 lety

    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.

  • @nakuldeshmukh7901
    @nakuldeshmukh7901 Před rokem

    this was super helpful !

  • @ayushdixit8993
    @ayushdixit8993 Před 4 lety

    You are awesome bro.... thanks for all these videos...

  • @israelor1986
    @israelor1986 Před 4 lety

    Hi kevin, do u have any suggestion how to.prepare for behaviour interview on facebook? I have one in few days. Thanks

  • @idriskas
    @idriskas Před 5 lety

    Hey great material. I subscribed! Do you know if the folks you talked to got their respective positions?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +4

      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!

  • @dipukrishnan6383
    @dipukrishnan6383 Před 3 lety

    @Kevin - am currently prepping for tech interview. Can you give me some areas to focus on? Can we talk? Thank you!

  • @prithvimd9091
    @prithvimd9091 Před 2 lety

    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?

  • @wl7497
    @wl7497 Před 5 lety

    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 ?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety

      Thanks and yes that sound about right to me :)

    • @code7434
      @code7434 Před 4 lety

      Time complexity is actually n^2 ,as u r shifting entire string to right in each iteration

  • @ihfaztajwar1632
    @ihfaztajwar1632 Před 4 lety +2

    Hey man I have a Facebook interview coming up and would love some advise.

  • @ShaliniNegi24
    @ShaliniNegi24 Před 3 lety

    Binary addition is the same as we do in case of decimal nos.

  • @davidquezada2116
    @davidquezada2116 Před 3 lety

    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?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 3 lety +1

      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

  • @Arupchakraborty2004
    @Arupchakraborty2004 Před 5 lety

    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.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +1

      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

  • @akmalbukhariev7932
    @akmalbukhariev7932 Před 2 lety

    Very good brother.

  • @AwaisKhan-iy8ix
    @AwaisKhan-iy8ix Před rokem

    Hi, Kevin I have an interview with Google on Monday. I want to talk to you and need your guidance

  • @prakhargandhi8919
    @prakhargandhi8919 Před 4 lety

    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

  • @treyquattro
    @treyquattro Před 4 lety

    is that a helicopter going by at 3:12?

  • @mxu2050
    @mxu2050 Před 4 lety

    sb. insert(0, c) is an O(N) operation. should we use sb. append(c) and then reverse it?

  • @bobxubo
    @bobxubo Před 5 lety

    thanks you much!

  • @chetanpatteparapu7600
    @chetanpatteparapu7600 Před 4 lety +2

    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.

    • @turskyi
      @turskyi Před 2 lety

      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.

    • @chetanpatteparapu7600
      @chetanpatteparapu7600 Před 2 lety

      @@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

  • @rohitgpt7201
    @rohitgpt7201 Před 3 lety

    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.

  • @aliciasuper7014
    @aliciasuper7014 Před 3 lety

    Thank you for the video :) I’ve a Facebook interview coming up, would love to talk to you

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 3 lety

      Same here! Check out my Patreon page for the tier that’d be the most helpful for you! www.patreon.com/KevinNaughtonJr

  • @kde6769
    @kde6769 Před 5 lety

    thank you Kevin! :D

  • @vk4081
    @vk4081 Před 4 lety

    Hi Kevin, I have Facebook interview coming up and would love to get your feedback.

  • @shaindholakiya5930
    @shaindholakiya5930 Před 4 lety

    Love your vids man! I have a Facebook internship interview coming up. Would love to connect and talk

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

      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

    • @shaindholakiya5930
      @shaindholakiya5930 Před 4 lety

      @@KevinNaughtonJr Joined!

  • @shashanksetty8110
    @shashanksetty8110 Před 4 lety +4

    how do you guys come up with the %2 and /2 thing I would have never thought of that. :(

    • @suyogsubedi8584
      @suyogsubedi8584 Před 3 lety

      after 1 year have you started to think like this?

    • @shashanksetty8110
      @shashanksetty8110 Před 3 lety +1

      @@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.

    • @suyogsubedi8584
      @suyogsubedi8584 Před 3 lety +1

      @@shashanksetty8110 ayy you got the job that is what matters. Currently in the same phase you were a year ago.

    • @shashanksetty8110
      @shashanksetty8110 Před 3 lety

      @@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

    • @suyogsubedi8584
      @suyogsubedi8584 Před 3 lety +1

      @@shashanksetty8110 thank you for the advice brother

  • @ahkim93
    @ahkim93 Před 3 lety

    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 ❤❤

  • @nandpatel924
    @nandpatel924 Před 3 lety +1

    When are you going to marry?
    Whats the time complexity for that program?

  • @sandipbhaumik
    @sandipbhaumik Před 4 lety

    I have an interview with one of FAANGs, can you please help?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      Sandip Bhaumik definitely! Check out my Patreon page and sign up for whatever will help you the most: www.patreon.com/KevinNaughtonJr

  • @anuragmishra2964
    @anuragmishra2964 Před 5 lety +2

    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

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +1

      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

    • @anuragmishra2964
      @anuragmishra2964 Před 5 lety

      thank you @@KevinNaughtonJr

    • @MadhurjyaBora
      @MadhurjyaBora Před 4 lety +2

      @@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.

  • @dev-skills
    @dev-skills Před 3 lety

    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));
    }

  • @antoniomartinez3429
    @antoniomartinez3429 Před 4 lety

    big O? thank you!!

  • @harigovind11
    @harigovind11 Před 3 lety

    I was asked this last week by Fb :)

    • @unknownman1
      @unknownman1 Před 3 lety

      you got the job bro?

    • @harigovind11
      @harigovind11 Před 3 lety

      @@unknownman1 cleared the tele rounds, waiting for on-site interview :)

  • @vk1618
    @vk1618 Před 4 lety

    Method to convert characters to integers

  • @JohnWick-zc5li
    @JohnWick-zc5li Před 5 lety +1

    it will fail for case 0 and 0

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety

      I tested my code against "0" and "0" and it passes :)

  • @venkatsai5013
    @venkatsai5013 Před 5 lety

    Awesome video.. Can you please help me

  • @Kayteck
    @Kayteck Před 2 lety

    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

  • @yuzhewang6746
    @yuzhewang6746 Před 3 lety

    from 2020.12

  • @asnajar
    @asnajar Před 4 lety

    Please help I need a serious help in solving problems :D

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      Check out my Patreon page! Might be a tier that you can join that would be helpful for you :)

  • @nealpatel7696
    @nealpatel7696 Před 5 lety

    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.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +1

      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

    • @nealpatel7696
      @nealpatel7696 Před 5 lety

      @@KevinNaughtonJr would you suggest EPI or CTCI to accompany it?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 5 lety +1

      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

    • @nealpatel7696
      @nealpatel7696 Před 5 lety

      @@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.