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!
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
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.
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.
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;
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.
@@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...
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.
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!
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 ..
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 !
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ú.
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.
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
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
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); } }
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 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 😁
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.
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.....
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.
now witness the birth of an amazing youtuber who will surpass Tushar Roy, Abdual Bari, GeeksforGeeks, Gaurav Sen, mycodeschool, william fiset and the likes.
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.
@@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
who wants a video series on Dynamic Programming? Do reply and smash that like button.
Omprakash Bairagi The big Yes
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)
13%15 is 13 not 0
Appreciate ur effort to start this coding series. Kindly also put spring boot security series in ur bucket list.
I am interested in this too! Thank you!
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
String output = ((i%3)==0)? (((i%5)==0)? "FizzBuzz" : "Fizz") : (((i%5)==0)? "Buzz" : String.valueOf(i));
Very nice
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!
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
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.
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.
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;
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.
@@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...
And what do you think appending the immutable Strings would do? Do you consider it to be an optimized solution?
Thanks for starting this new series. We need more problems like this and correct approach when we try to solve a problem.
I just now found your channel...you are so much awesome bro... thanks a lot for information sharing....I highly appreciate your work......
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.
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.
@@Java.Brainsother than that, it's a pretty trivial problem. Looking forward to more of these videos! :D
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!
Exactly !
@@qo92 Nice.
You can eliminate the brackets on the for loop.
Very best initiative. Programming puzzles are the most expected questions in java interview. Thanks for the series!
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 ..
Wow, more exciting stuff from the legend himself... #Kaushik'sNo1Fan
Another approach:
for(i=1;i
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 !
Great efforts Koushik, love all ur videos on spring, spring boot, Java 8, microservices. Eagerly waiting for microservicervices Level 2 series
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ú.
You could also have written the first if in a shorter way: if (i % 15 == 0)
for java the best solution is to import FizzBuzz library and then run it's print(int n) method
Looking forward for more complex coding questions. Thanks for starting this series.
Thank you! The way you explain is in believable:). Could you try to make a video on Big O notation?
Why don't you do a series on Core Java?
waiting for microservice level 2 :P
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.
Post more coding challenges on queues , lists and maps please
Please upload more problems which are complex
Nice Video Sir... Can you explain how to solve Happy Number Problem?
Great 👍
Can we have a solution for the minimised number of characters!??
For that write code in python if you want to score more.
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
Can you please solve dynamic programming problems?
Great sir keep doing......
Thank You 🥰
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
Hey @ Kaushik please continue your videos ..I m waiting for a long time
Sir,pls make a project using core java,advance java.series
Let them coming. We want more videos, sir.
perfect thank you
Ty!
please continue this series
you are superb... can you please post such solutions
Hello Koushik sir will you please create playlist.
Waiting for DS and Alogthims.
Design patterns in java.
Here's the playlist. More videos to come soon. ruclips.net/p/PLqq-6Pq4lTTZgXnsBNQwCWdKR6O6Cgk1Z
Java Puzzlers is a great book .
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);
}
}
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..
if(i % 15 == 0) also works instead of &&
can you please explain "Infinite Recursion problem when working with Jackson" in spring boot JPA joins?
Thank you for all the tutorials. Eagerly waiting for the next series.
You can explain problems from how to crack coding interview book.
Well, I appreciate your effort, Sir. It would be good if you daily post one video. :)
I would do this:
public static void main (String[] args) {
for(int i=1; i
I like the thought, but why startsWith? You could just ensure print starts as an empty string and do print += "Buzz" :)
thank u so much
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
what are you trying to do ?
@@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 😁
@@tbm5k OK.
thanks
Can you do this same for Javascript as well like interviewing questions for Javascript?
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.
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.....
No more videos about microservices? :(
Did you tackle Spring boot course?
Using two else's seems a bit ugly. Isn't using 'continue' better - as in easier to read?
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.
now witness the birth of an amazing youtuber who will surpass Tushar Roy, Abdual Bari, GeeksforGeeks, Gaurav Sen, mycodeschool, william fiset and the likes.
Are you teaching JavaScript? Thanks for awesome tutorial. I'm creating video like you on my channel. Thanks
Are experienced java developers asked to solve coding challenges?
I dont get how you can get this wrong?
Am shocked that people with multiple years of experience couldnt solve this.
Because this is actually not the ideal solution to the problem.
Sir what about this? short and sweet isn't it?
for (int i = 1; i
This is also a possible solution, but, IMHO, you have traded in readability for less verbose code.
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.
@@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
The question is too easy.
I did this in 2 minutes
public void printNFB() {
boolean number=true;
for(int i=1;i
I got this wrong :(
public static void main(String[] args) {
for (int i = 1; i
Easy
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);
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)
}