Java > Array-2 > fizzBuzz (CodingBat Solution)

Problem:

This is slightly more difficult version of the famous FizzBuzz problem which is sometimes given as a first problem for job interviews. (See also: FizzBuzz Code.) Consider the series of numbers beginning at start and running up to but not including end, so for example start=1 and end=5 gives the series 1, 2, 3, 4. Return a new String[] array containing the string form of these numbers, except for multiples of 3, use "Fizz" instead of the number, for multiples of 5 use "Buzz", and for multiples of both 3 and 5 use "FizzBuzz". In Java, String.valueOf(xxx) will make the String form of an int or other type. This version is a little more complicated than the usual version since you have to allocate and index into an array instead of just printing, and we vary the start/end instead of just always doing 1..100.

fizzBuzz(1, 6) → {"1", "2", "Fizz", "4", "Buzz"}
fizzBuzz(1, 8) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7"}
fizzBuzz(1, 11) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"}


Solution:

public String[] fizzBuzz(int start, int end) {
    int n = end - start;
    String[] result = new String[n];
 
    int pos = 0;
    for (int i = start; i < end; i++) {
        boolean fizz = i % 3 == 0;
        boolean buzz = i % 5 == 0;
 
        if (fizz && buzz) result[pos] = "FizzBuzz";
        else if (fizz) result[pos] = "Fizz";
        else if (buzz) result[pos] = "Buzz";
        else result[pos] = String.valueOf(i);
        pos++;
    }
    return result;
}


12 comments :

  1. public String[] fizzBuzz(int start, int end) {
    String[] result = new String[end - start];
    for (int i = start; i < end; i++){
    if(i % 3 != 0 && i % 5 != 0){
    result[i - start] = String.valueOf(i);
    }
    if(i % 3 == 0){
    result[i - start] = "Fizz";
    }
    if(i % 5 == 0){
    result[i - start] = "Buzz";
    }
    if(i % 3 == 0 && i % 5 == 0){
    result[i - start] = "FizzBuzz";
    }
    } return result;
    }

    ReplyDelete
  2. String[] nwarr = new String[end-start];

    for(int i=0; i<nwarr.length; i++){
    if(start % 3 == 0 && start % 5 == 0) nwarr[i] = "FizzBuzz";
    else if(start % 5 == 0) nwarr[i] = "Buzz";
    else if(start % 3 == 0) nwarr[i] = "Fizz";
    else {
    nwarr[i] = String.valueOf(start);
    }
    start++;
    }

    return nwarr;

    ReplyDelete
  3. int diff = end - start;

    String[] res = new String[diff];
    int index = 0;
    for (int i = start; i < end; i++) {
    if (i % 5 == 0 && i % 3 == 0) {
    res[index] = "FizzBuzz";
    index++;
    }
    else if (i % 3 == 0) {
    res[index] = "Fizz";
    index++;
    } else if (i % 5 == 0) {
    res[index] = "Buzz";
    index++;
    } else {
    res[index] = String.valueOf(i);
    index++;
    }
    }
    return res;
    }

    ReplyDelete
  4. public String[] fizzBuzz(int start, int end) {
    String[] result = new String[end-start];
    for (int i = start; i < end; i++) {
    if ( i%3 == 0 && i%5 == 0 ) {
    result[i-start] = "FizzBuzz";
    } else if ( i%3 == 0 ) {
    result[i-start] = "Fizz";
    } else if ( i%5 == 0 ) {
    result[i-start] = "Buzz";
    } else {
    result[i-start] = String.valueOf(i);
    }
    }
    return result;
    }

    ReplyDelete
  5. public String[] fizzBuzz(int start, int end) {


    String[] fizzB = new String[end-start];


    for (int i = start, j=0; i < end; i++, j++) {

    fizzB[j] = String.valueOf(i);

    if (i % 3 == 0 && i % 5 == 0) {

    fizzB[j] = "FizzBuzz";

    } else if (i % 3 == 0) {
    fizzB[j] = "Fizz";

    } else if (i % 5 == 0) {

    fizzB[j] = "Buzz";

    }

    }

    return fizzB;

    ReplyDelete
  6. String arr[] = new String[end-start];
    for(int i = 0;i<arr.length;i++)
    {
    arr[i] = String.valueOf(start+i);
    if((start+i)%3==0)arr[i] = "Fizz";
    if((start+i)%5==0)arr[i] = arr[i].replaceAll("\\d","") + "Buzz";
    }
    return arr;

    ReplyDelete
  7. public String[] fizzBuzz(int start, int end) {
    String[] ret = new String[end - start];
    for (int i = start; i < end; i++)
    ret[i - start] = i % 3 == 0 ? (i % 5 == 0 ? "FizzBuzz" : "Fizz") : (i % 5 == 0 ? "Buzz" : String.valueOf(i));
    return ret;
    }

    ReplyDelete
  8. public String[] fizzBuzz(int start, int end) {
    String[] res = new String[end-start];
    for (int i=0; i<res.length; i++) {
    if (start%3==0) {
    if (start%5==0) res[i] = "FizzBuzz";
    else res[i] = "Fizz";
    }
    else if (start%5==0) res[i] = "Buzz";
    else res[i] = String.valueOf(start);
    start++;
    }
    return res;
    }

    ReplyDelete
  9. public String[] fizzBuzz(int start, int end) {
    String[] result = new String[end-start];

    for(int i=start; i<end; i++){
    String value = "";

    if(i%3==0) value += "Fizz";
    if(i%5==0) value += "Buzz";
    if(value=="") value = i+"";

    result[i-start]=value;
    }

    return result;
    }

    ReplyDelete
  10. public String[] fizzBuzz(int start, int end) {
    String[] result = new String[end - start];
    for (int i = start; i < end; i++){
    if(i % 15 != 0){
    result[i - start] = String.valueOf(i);
    }
    if(i % 3 == 0){
    result[i - start] = "Fizz";
    }
    if(i % 5 == 0){
    result[i - start] = "Buzz";
    }
    if(i % 15 == 0){
    result[i - start] = "FizzBuzz";
    }
    } return result;
    }

    ReplyDelete
  11. With Java Stream

    public static String[] fizzBuzz(int start, int end) {
    String[] fb = IntStream.range(start, end)
    .boxed()
    .map(i -> {
    if (i % (3 * 5) == 0) {
    return "FizzBuzz";
    } else if (i % 3 == 0) {
    return "Fizz";
    } else if (i % 5 == 0) {
    return "Buzz";
    } else {
    return Integer.toString(i);
    }
    })
    .toArray(String[]::new);

    return fb;
    }

    ReplyDelete
  12. public String[] fizzBuzz(int start, int end) {
    String str[] = new String[end-start];

    for(int i=0 ; i<str.length ;i++){

    if(start%3==0 && start%5==0){
    str[i] = "FizzBuzz";
    }
    else if(start%3==0 ){
    str[i] = "Fizz";
    }
    else if( start%5==0){
    str[i] = "Buzz";
    }
    else {
    str[i] = String.valueOf(start);
    }
    start++;
    }

    return str;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact