Java Interview Coding Challenge #1: FizzBuzz

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 109

  • @omprakashbairagi1555
    @omprakashbairagi1555 5 лет назад +54

    who wants a video series on Dynamic Programming? Do reply and smash that like button.

  • @vaibhavpatil_it
    @vaibhavpatil_it 5 лет назад +30

    How about put i%15 instead of (i%3)&&(i%5)
    It will be more efficient.
    For extension friendly code we can write i%(3*5)

  • @chandramanigupta6997
    @chandramanigupta6997 5 лет назад +51

    Appreciate ur effort to start this coding series. Kindly also put spring boot security series in ur bucket list.

    • @payalkamble9920
      @payalkamble9920 5 лет назад +2

      I am interested in this too! Thank you!

    • @SEVERANCE850
      @SEVERANCE850 3 года назад

      I think he is more of development side and not a coder type because i think he do not have deep knowledge of ds algp

  • @BharCode09
    @BharCode09 5 лет назад +7

    String output = ((i%3)==0)? (((i%5)==0)? "FizzBuzz" : "Fizz") : (((i%5)==0)? "Buzz" : String.valueOf(i));

  • @__Pathfinder__
    @__Pathfinder__ 5 лет назад +1

    instead of i % 3 == 0 && i %5 == 0 you could just write i %15 == 0 System.out.println("FizzBuzz")
    just the multiplication of the two numbers.
    It's a bit shorter and more elegant code.
    Thank's alot for you very good videos. You are a great teacher!

  • @ramgopal.cheedureddy9658
    @ramgopal.cheedureddy9658 5 лет назад

    This type of problem statements are fairly simple if you know how to use modulus and division arthamatic operators. People look for O(1) complexity. I came across similar problem where you need to calculate lowest number of coin required to split a given dollar amount.
    Example you ill be given following coins $1, $0.25, $0.10, $0.1 and calculate least amount of coins required to split $5.58.
    Outcome : 5 - $1 coins, 2 - 25 cents, 8 - 1 cent coins

  • @heksqer1022
    @heksqer1022 5 лет назад +14

    This is *not* the ideal solution that interviewers look for. (Well, from what I know ). There are better ways of achieving the same result with less code and more efficiency.
    The problem with this approach is it's hard to expand. So if you were to make it so that you need to print 'Fuzz' every time a multiple of 7 shows up and FizzBuzzFuzz every time a multiple of 3 , 5 and 7 shows up, you need to heavily modify the code.
    Something else that you could do is have a String variable called output and set it to nothing.
    String output = "";
    then just do this :
    if(i%3 == 0) , output += "Fizz";
    if(i%5 == 0) , output += "Buzz";
    print(output);
    This approach is very easy to expand and customize. Also it's much shorter and concise. So now if you need to add 2 more conditions for 7 and 11, all you need to do is add this before the print statement :
    if(i%7 == 0) , output += "Fuzz";
    if(i%11 == 0) , output += "Jazz";
    I personally think this is somewhat better and you don't have to worry about the problem you mentioned in the video. Then again, both work fine. Cheers.

    • @agdevoq
      @agdevoq 5 лет назад +1

      You should also manage the "else" condition, using
      Print(output.isEmpty()?""+i:output)
      Also, you can save tons of chars by contracting "if" into "?:" operators.

    • @f3rdi881
      @f3rdi881 5 лет назад +1

      You are correct just you need to test if output is empty, with your code currently it will print blank for any number not a multiple of 3 or 5.
      So another if statement is needed,
      If(output.equals("")){ output = i;}
      Print output;

    • @blasttrash
      @blasttrash 5 лет назад +1

      This is whats called as premature optimization. Without knowing the context of what interviewer is expecting, this could or could not be the ideal solution. Also string concatenation is bad. Use something like stringbuilder if you want to worry so much about premature optimization.

    • @agdevoq
      @agdevoq 5 лет назад

      @@blasttrash Actually, the context is very clear: write down the code in the shortest way possible, keystroke-wise. Then you can expand your interview talking about StringBuilders etc...

    • @mushifalinawaz
      @mushifalinawaz 5 лет назад

      And what do you think appending the immutable Strings would do? Do you consider it to be an optimized solution?

  • @ssrujithreddy
    @ssrujithreddy 5 лет назад +1

    Thanks for starting this new series. We need more problems like this and correct approach when we try to solve a problem.

  • @madhavanrao3626
    @madhavanrao3626 5 лет назад +1

    I just now found your channel...you are so much awesome bro... thanks a lot for information sharing....I highly appreciate your work......

  • @azizthanawala7670
    @azizthanawala7670 5 лет назад +5

    The challenge isn't to write a code for the problem, it is to reduce the solution code provided in the test challenge as much as possible.

    • @Java.Brains
      @Java.Brains  5 лет назад +4

      Yup, I realize that’s what the HackerRank problem states - code with minimal number of characters. I just used that page to define the FizzBuzz problem.
      Character limit for code isn’t really applicable in an interview context anyway.

    • @azizthanawala7670
      @azizthanawala7670 5 лет назад

      @@Java.Brainsother than that, it's a pretty trivial problem. Looking forward to more of these videos! :D

    • @qo92
      @qo92 5 лет назад +3

      Something like this maybe?
      public class F{public static void main(String[]a){for(int i=0;i score of 0.54)
      The same in Groovy:
      (1..100).each{i->println((i%3?"":"Fizz")+(i%5?(i%3?i:""):"Buzz"))}
      (67 chars => score of 1.33)
      I'm sure it can be improved on though!

    • @heksqer1022
      @heksqer1022 5 лет назад

      Exactly !

    • @ShinAkuma
      @ShinAkuma 4 года назад

      @@qo92 Nice.
      You can eliminate the brackets on the for loop.

  • @mayoorm909
    @mayoorm909 5 лет назад

    Very best initiative. Programming puzzles are the most expected questions in java interview. Thanks for the series!

  • @deepak9976
    @deepak9976 5 лет назад

    Cannot thank enough to Kaushik kothagal , for crystal clear concepts, showing paths and keeping the things so exciting. His videos, together with books can make programming so much fun and ideas can keep flowing ..

  • @skullwise
    @skullwise 5 лет назад +1

    Wow, more exciting stuff from the legend himself... #Kaushik'sNo1Fan

  • @inspiration7
    @inspiration7 5 лет назад +2

    Another approach:
    for(i=1;i

  • @anshuldogra7607
    @anshuldogra7607 5 лет назад

    I was wondering sometime back how it would be to get to learn how to tackle interview coding questions from you and here you are with a brand new series for the same. Kudos to you Koushik !

  • @srikanthdannarapu6678
    @srikanthdannarapu6678 5 лет назад +1

    Great efforts Koushik, love all ur videos on spring, spring boot, Java 8, microservices. Eagerly waiting for microservicervices Level 2 series

  • @Renso2010
    @Renso2010 3 года назад

    Thank you for sharing this information, I appreciated your work, this tips are very important all programmers because with this knowgle is easier to prepare and anticipate future questions. Regards from Perú.

  • @Giorgi.Japiashvili
    @Giorgi.Japiashvili Год назад +1

    You could also have written the first if in a shorter way: if (i % 15 == 0)

  • @FatovMikhail
    @FatovMikhail 5 лет назад

    for java the best solution is to import FizzBuzz library and then run it's print(int n) method

  • @sharidass1408
    @sharidass1408 5 лет назад

    Looking forward for more complex coding questions. Thanks for starting this series.

  • @surves
    @surves 5 лет назад +1

    Thank you! The way you explain is in believable:). Could you try to make a video on Big O notation?

  • @poornaps
    @poornaps 4 года назад +1

    Why don't you do a series on Core Java?

  • @DhrumilShahDOTin
    @DhrumilShahDOTin 5 лет назад +8

    waiting for microservice level 2 :P

  • @priyankawagh5217
    @priyankawagh5217 4 года назад

    Thanks Kaushik for this tutorial series. Can You please also make series of tutorials for CompletableFuture In java8? it's the topic I didn't find in your video series for Java8 features.

  • @premanand3810
    @premanand3810 4 года назад

    Post more coding challenges on queues , lists and maps please

  • @varunbhardwaj2267
    @varunbhardwaj2267 5 лет назад +2

    Please upload more problems which are complex

  • @rajnayanpalav6425
    @rajnayanpalav6425 5 лет назад

    Nice Video Sir... Can you explain how to solve Happy Number Problem?

  • @sahilGupta217
    @sahilGupta217 5 лет назад +2

    Great 👍
    Can we have a solution for the minimised number of characters!??

    • @PrabalTiwarii
      @PrabalTiwarii 4 года назад +1

      For that write code in python if you want to score more.

  • @giriprasadp1726
    @giriprasadp1726 Год назад

    Thank you for the explanation....I just reworked in another way.Will it be ok?
    public class Main {
    private static final int N = 0;
    public static void main(String[] args){
    int i=N;
    for(int N=1;N

  • @VishwanathGowdar
    @VishwanathGowdar 5 лет назад +1

    Can you please solve dynamic programming problems?

  • @ravikiran8174
    @ravikiran8174 5 лет назад

    Great sir keep doing......

  • @Laprincesavampire
    @Laprincesavampire 2 года назад

    Thank You 🥰

  • @andreicioropar8472
    @andreicioropar8472 5 месяцев назад

    This problem reminds me of a game i used to play when i was young with 3 and 7 with a group of friends. We were counting one by one and every time when someone missed the BUZZ ( in our country we would call it BOLTZ instead of BUZZ :D ) had to drink the BOOZE. Nice job anyway

  • @DeepakVerma-bz6md
    @DeepakVerma-bz6md 5 лет назад

    Hey @ Kaushik please continue your videos ..I m waiting for a long time

  • @parvezmd6455
    @parvezmd6455 5 лет назад +2

    Sir,pls make a project using core java,advance java.series

  • @aakashbhardwaj476
    @aakashbhardwaj476 5 лет назад

    Let them coming. We want more videos, sir.

  • @mizoo7617
    @mizoo7617 2 года назад

    perfect thank you

  • @arch92
    @arch92 3 года назад

    Ty!

  • @toufiqurrahman7025
    @toufiqurrahman7025 5 лет назад

    please continue this series

  • @abhaygirwalkar7611
    @abhaygirwalkar7611 5 лет назад

    you are superb... can you please post such solutions

  • @sirigiris5062
    @sirigiris5062 5 лет назад

    Hello Koushik sir will you please create playlist.
    Waiting for DS and Alogthims.
    Design patterns in java.

    • @Java.Brains
      @Java.Brains  5 лет назад

      Here's the playlist. More videos to come soon. ruclips.net/p/PLqq-6Pq4lTTZgXnsBNQwCWdKR6O6Cgk1Z

  • @jonassx100
    @jonassx100 5 лет назад

    Java Puzzlers is a great book .

  • @mamad_reza74
    @mamad_reza74 Год назад

    nice but i found solution with three if :
    private static void printFizzBuzz(int[] input) {
    for (int n : input){
    StringBuilder s = new StringBuilder();
    if (n%3==0){
    s.append("Fizz");
    }
    if(n%5==0){
    s.append("Buzz");
    }
    if(s.isEmpty())s.append(n);
    System.out.println(s);
    }
    }

  • @hyperslowloris
    @hyperslowloris 5 лет назад

    It seems that score is based on characters..So instead of using i%3==0 && i%5==0, Why not use i%15==0 ?? LCM basics right?? Correct me if I am wrong..

  • @DriveandThrive
    @DriveandThrive 2 года назад

    if(i % 15 == 0) also works instead of &&

  • @arvindynr
    @arvindynr 4 года назад

    can you please explain "Infinite Recursion problem when working with Jackson" in spring boot JPA joins?

  • @AbsTht
    @AbsTht 5 лет назад

    Thank you for all the tutorials. Eagerly waiting for the next series.

  • @ajaykumar-xy6pw
    @ajaykumar-xy6pw 5 лет назад

    You can explain problems from how to crack coding interview book.

  • @amarjitkumarsingh25
    @amarjitkumarsingh25 5 лет назад

    Well, I appreciate your effort, Sir. It would be good if you daily post one video. :)

  • @rajay2r
    @rajay2r 5 лет назад

    I would do this:
    public static void main (String[] args) {
    for(int i=1; i

    • @Java.Brains
      @Java.Brains  5 лет назад +1

      I like the thought, but why startsWith? You could just ensure print starts as an empty string and do print += "Buzz" :)

  • @darshanabhuyan5896
    @darshanabhuyan5896 2 года назад

    thank u so much

  • @tbm5k
    @tbm5k 5 лет назад

    Hello, in the Spring boot course, you forgot to teach us how to link mySQL to the API. I have tried it myself but the field with the foreign key is not being filled with data

    • @melvinkimathi8924
      @melvinkimathi8924 3 года назад

      what are you trying to do ?

    • @tbm5k
      @tbm5k 3 года назад

      @@melvinkimathi8924 i was trying make a relationship btn two records but the relationship wasnt being made, I got a hang of it though 😁. At the time was new to coding so it was quite confusing. Thank you for offering some helping hand yet it is over a year later 😁

    • @melvinkimathi8924
      @melvinkimathi8924 3 года назад

      @@tbm5k OK.

  • @GenjaOrigins
    @GenjaOrigins 3 года назад

    thanks

  • @battootoko9307
    @battootoko9307 5 лет назад

    Can you do this same for Javascript as well like interviewing questions for Javascript?

  • @Blure
    @Blure 5 лет назад

    Hello sir, I do hope my message finds you well. I would like to ask: Is it worth to learn Spring in 2019? Thank you in advance and thanks for sharing your knowledge.

    • @niyaziozturk3967
      @niyaziozturk3967 4 года назад

      I can answer your question. Firstly, yes, learn it, there are still plenty of applications around built and will be built with Spring. Secondly, if you know how the Spring framework works, then knowing the system of the MVC framework helps you pick up other frameworks easily and quicker. Third, you aim to think of systematic thinking, see the big picture of any systems, and the details in it. See the tree and the forest at the same time, tell about them whichever the detail is required.....

  • @francogb
    @francogb 5 лет назад +1

    No more videos about microservices? :(

    • @tbm5k
      @tbm5k 5 лет назад

      Did you tackle Spring boot course?

  • @domaataajs
    @domaataajs 5 лет назад

    Using two else's seems a bit ugly. Isn't using 'continue' better - as in easier to read?

    • @Java.Brains
      @Java.Brains  5 лет назад +1

      That's subjective. I prefer multiple if-else to a bunch of continues and breaks. The latter makes it hard to read and reason the flow (same argument against the primitive go-to statements.

  • @blasttrash
    @blasttrash 5 лет назад

    now witness the birth of an amazing youtuber who will surpass Tushar Roy, Abdual Bari, GeeksforGeeks, Gaurav Sen, mycodeschool, william fiset and the likes.

  • @techbarikcom
    @techbarikcom 4 года назад

    Are you teaching JavaScript? Thanks for awesome tutorial. I'm creating video like you on my channel. Thanks

  • @ranjitdesai3926
    @ranjitdesai3926 Год назад

    Are experienced java developers asked to solve coding challenges?

  • @John-Is-My-Name
    @John-Is-My-Name 7 месяцев назад

    I dont get how you can get this wrong?

  • @she.....
    @she..... 5 лет назад

    Am shocked that people with multiple years of experience couldnt solve this.

    • @heksqer1022
      @heksqer1022 5 лет назад

      Because this is actually not the ideal solution to the problem.

  • @SushilKumarBhaskar
    @SushilKumarBhaskar 5 лет назад

    Sir what about this? short and sweet isn't it?
    for (int i = 1; i

    • @skullwise
      @skullwise 5 лет назад

      This is also a possible solution, but, IMHO, you have traded in readability for less verbose code.

    • @heksqer1022
      @heksqer1022 5 лет назад +1

      This works but it can't be expanded to let's say FizzBuzzCuzz for when you are also counting multiples of 7 along with 3 and 5. And it's not very readable.
      But hey, it works.

    • @SushilKumarBhaskar
      @SushilKumarBhaskar 5 лет назад

      @@heksqer1022 Question was, to Write a solution (or reduce an existing one) so it has as few characters as possible. [ why don't you try to beat my score in hacker rank in below URL,mine score was only 6.5 out of 20 in Java ] URL : www.hackerrank.com/challenges/fizzbuzz/problem

  • @julielong6936
    @julielong6936 5 лет назад +1

    The question is too easy.

  • @waxzodarasulmetova2749
    @waxzodarasulmetova2749 4 года назад

    I did this in 2 minutes

  • @malinmalindic8582
    @malinmalindic8582 4 года назад

    public void printNFB() {
    boolean number=true;
    for(int i=1;i

  • @YouTubeisZionistTool
    @YouTubeisZionistTool 4 года назад

    I got this wrong :(

  • @asankasiriwardena3383
    @asankasiriwardena3383 4 года назад

    public static void main(String[] args) {
    for (int i = 1; i

  • @michaeleduardogarciaabello2180
    @michaeleduardogarciaabello2180 5 лет назад

    Easy

  • @RIJINPK
    @RIJINPK Год назад

    more precise one:
    IntStream.rangeClosed(1, 100).boxed().map(elem -> elem % 3 == 0 && elem % 5 == 0 ? "FizzBuzz" : elem % 3 == 0 ? "Fizz" : elem % 5 == 0 ? "Buzz" : elem).forEach(System.out::println);

  • @AlixsaBlaer
    @AlixsaBlaer Год назад

    for(let i = 1;i < 100;i++){
    i % 5=== 0 && i % 3 === 0 ?console.log("FizzBuzz "+i):i % 3 === 0 ? console.log("Frizz "+i) : i % 5 === 0 ? console.log("Buzz "+i) :console.log(i)
    }