Sunday, November 24, 2019

How To Solve Fizzbuzz Inwards Java?

FizzBuzz is 1 of the most oftentimes asked questions on programming interviews too used to filter programmers who can't program. It looks extremely uncomplicated but it's tricky for those programmers or coder who create create to construction their code. Fizzbuzz work tilt is rattling simple, write a computer programme which furnish "fizz" if the discover is a multiplier of 3, furnish "buzz" if its multiplier of 5 too furnish "fizzbuzz" if the discover is divisible yesteryear both 3 too 5. If the discover is non divisible yesteryear either 3 or 5 thus it should simply furnish the discover itself. You tin flaming see nix is fancy when it comes to thinking most the solution, but when you lot start coding, you lot volition see a work amongst structuring your code, especially if else blocks.

In this article, nosotros volition become through a duad of solution to empathize the crux of this problem. If you lot are learning Java 8, thus you lot tin flaming besides solve this work using lambda aspect too streams, every bit shown here.

Btw, If you lot are preparing for programming project interviews too looking for some coding problems to cook well, thus I volition recommend you lot to become through problems given inwards the book, Cracking the Coding Interview. It contains closed to 150 programming questions too their solutions amongst a proficient explanation.


First solution of FizzBuzz Problem

Many programmers come upwards up amongst the next solution when showtime faced amongst Fizzbuzz work :
 public static String fizzBuzz(int number) {         if (number % 3 == 0) {             if (number % 5 == 0) {                 return "fizzbuzz";             } else {                 return "fizz";             }         } else if (number % 5 == 0) {             return "buzz";         }         return String.valueOf(number);     }


If you lot await at this solution closely you lot volition uncovering that the construction of the if statements is non good, too in that place are 2 tests for the same status i.e. 2 if block to cheque if the discover is divisible yesteryear 5. The work is the code is non proficient but you lot should order the create goodness of the dubiousness to the programmer because fifty-fifty though a exam of 5 is written twice they are inwards dissimilar branches, too solely 1 of them volition execute, but I concord the code is non clean.



Second solution of FizzBuzz

They telephone commutation of coming amongst perfect solution is coming upwards amongst an approach to create the exam for divisible yesteryear both 3 too 5 first. This brings to a greater extent than readability without duplication, every bit shown here
public static String fizzBuzz2(int number) {          if (number % 15 == 0) {             return "fizzbuzz";         } else if (number % 5 == 0) {             return "buzz";         } else if (number % 3 == 0) {             return "fizz";         }         return String.valueOf(number);  }

You tin flaming see that it's much cleaner fifty-fifty though it is besides doing the exam twice, but it's to a greater extent than pleasing to the oculus too straightforward.

 is 1 of the most oftentimes asked questions on  How to solve FizzBuzz inwards Java?


Java Program to solve FizzBuzzProblem

Here is our consummate Java computer programme which combines both approaches to solving the fizzbuzz problem. We besides select some unit of measurement tests to verify our solution meets the work statements.
import org.junit.Test; import static org.junit.Assert.*; /**  * Java Program to solve FizzBuzz work  *   * @author WINDOWS 8  *  */ public class FizzBuzzTest {      /**      * Java Method to solve FizzBuzz problem, which states that computer programme      * should impress fizz if discover is multiple of 3,       * impress buzz if discover is multiple      * of 5, too impress fizzbuzz if discover is multiple of both 3 too 5      *       * @param discover      * @return      */     public static String fizzBuzz(int number) {          if (number % 3 == 0) {             if (number % 5 == 0) {                 return "fizzbuzz";             } else {                 return "fizz";             }         } else if (number % 5 == 0) {             return "buzz";         }         return String.valueOf(number);     }      /**      * An improved version of before solution, much cleaner than previous      * version because nosotros select tested for divisible yesteryear 3 too 5 first.      * It avoid duplication also.      */     public static String fizzBuzz2(int number) {         if (number % 15 == 0) {             return "fizzbuzz";         } else if (number % 5 == 0) {             return "buzz";         } else if (number % 3 == 0) {             return "fizz";         }         return String.valueOf(number);     }               @Test     public void testFizzBuzz(){         assertEquals("fizz", fizzBuzz(3));         assertEquals("buzz", fizzBuzz(5));         assertEquals("fizzbuzz", fizzBuzz(15));         assertEquals("2", fizzBuzz(2));     }          @Test     public void testFizzBuzzV2(){         assertEquals("fizz", fizzBuzzV2(3));         assertEquals("buzz", fizzBuzzV2(5));         assertEquals("fizzbuzz", fizzBuzzV2(15));         assertEquals("2", fizzBuzzV2(2));     } }  Test Result : All Pass 

That's all most how to solve FizzBuzz Problem inwards Java.  Sometime FizzBuzz is besides asked every bit next work statement, write a computer programme that prints the numbers from 1 to 100. But for multiples of 3 impress “Fizz” instead of the discover too for the multiples of v impress “Buzz”. For numbers which are multiples of both 3 too v impress “FizzBuzz". So don't confuse there, same logic plant fine.


Related Coding Problems from Programming Interviews
If you lot are a calculator scientific discipline graduate too serious most doing good on coding based questions on programming project interviews, endeavour to solve next problems:
  • How to cheque if a given discover is prime number or not? (solution)
  • Write a computer programme to impress the highest frequency discussion from a text file? (solution)
  • How to uncovering if given String is a palindrome inwards Java? (solution)
  • How to calculate factorial using recursion too iteration? (solution)
  • How to opposite an int variable inwards Java? (solution)
  • How to uncovering the highest too lowest discover from int array? (answer)
  • How create you lot swap 2 integers without using a temporary variable? (solution)
  • Top 10 Programming problems from Java Interviews? (article)
  • Write a computer programme to cheque if a discover is the might of 2 or not? (solution)
  • How to cheque if a twelvemonth is a leap twelvemonth inwards Java? (answer)
  • How to opposite String inwards Java without using StringBuffer? (solution)
  • Write code to implement Bubble form algorithm inwards Java? (code)
  • How create you lot opposite discussion of a judgement inwards Java? (solution)
  • Write code to implement Quicksort algorithm inwards Java? (algorithm)
  • How to uncovering a missing discover inwards a sorted array? (solution)
  • Write a computer programme to code insertion form algorithm inwards Java (program)


Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures too Algorithms: Deep Dive Using Java
Algorithms too Data Structures - Part 1 too 2

No comments:

Post a Comment