Solved

FizzBuzz challenge

Posted on 2016-08-14
9
77 Views
Last Modified: 2016-08-16
Hi,

I am working on below challenge

http://codingbat.com/prob/p153059

My psuedo code of the logical approach is
0. construct array within int fromm start till end
1. loop throguh given array
2. find given element is multiple of 3 if yes replace by fizz
3.  find given element is multiple of 5 if yes replace by Buzz
4. return modified array



I wrote my code as below

public String[] fizzBuzz(int start, int end) {
  int [] arr=null;
  for(int i=start;i<(end-1);i++){
    arr[i]=i;
  
  
  if(arr[i]%3==0){
    arr[i]="Fizz";
  }
 else if(arr[i]%5==0){
    arr[i]="Buzz";
  }
  
  }
  return arr;
}

Open in new window




I am not passing all tests

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 6
  • 2
9 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 41756344
Go reread the challenge.  You are missing at least 2 key parts.

mlmcc
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756522
else if (arr[i]%5==0&&(arr[i]%5==0){
    arr[i]="FizzBuzz";
  }

Open in new window

i see above condition
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756701
public class FizzBuzzChallenge {
	public static void main(String[] args) {
		System.out.println("value is-->"+fizzBuzz(1, 6));
	}

	public static String[] fizzBuzz(int start, int end) {
		  String [] arr=null;
		  for(int i=start;i<(end-1);i++){
		    arr[i]=String.valueOf(i);
		  
		  
		  if(  (Integer.parseInt(arr[i]) ) %3==0){
		    arr[i]="Fizz";
		  }
		 else if((Integer.parseInt(arr[i]) )%5==0){
		    arr[i]="Buzz";
		  }
		 else if( (Integer.parseInt(arr[i]) )%3==0  && (Integer.parseInt(arr[i]) )%5==0){
			    arr[i]="FizzBuzz";
			  }
		  }
		  return arr;
		}

}

Open in new window


i am getting NPE as below


Exception in thread "main" java.lang.NullPointerException
      at FizzBuzzChallenge.fizzBuzz(FizzBuzzChallenge.java:10)
      at FizzBuzzChallenge.main(FizzBuzzChallenge.java:4)


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

Open in new window


Expected      Run            
fizzBuzz(1, 6) → ["1", "2", "Fizz", "4", "Buzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1, 8) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1, 11) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1, 16) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1, 4) → ["1", "2", "Fizz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1, 2) → ["1"]      null      X      
fizzBuzz(50, 56) → ["Buzz", "Fizz", "52", "53", "Fizz", "Buzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(15, 17) → ["FizzBuzz", "16"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(30, 36) → ["FizzBuzz", "31", "32", "Fizz", "34", "Buzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(1000, 1006) → ["Buzz", "1001", "Fizz", "1003", "1004", "FizzBuzz"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(99, 102) → ["Fizz", "Buzz", "101"]      Exception:java.lang.NullPointerException (line number:4)      X      
fizzBuzz(14, 20) → ["14", "FizzBuzz", "16", "17", "Fizz", "19"]      Exception:java.lang.NullPointerException (line number:4)      X      
other tests

How to resolve this issue?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756720
public String[] fizzBuzz(int start, int end) {
		  String [] arr=new String[(end-start)];
		  for(int i=start;i<(end-1);i++){
		    arr[i]=String.valueOf(i);
		  
		  
		  if(  (Integer.parseInt(arr[i]) ) %3==0){
		    arr[i]="Fizz";
		  }
		 else if((Integer.parseInt(arr[i]) )%5==0){
		    arr[i]="Buzz";
		  }
		 else if( (Integer.parseInt(arr[i]) )%3==0  && (Integer.parseInt(arr[i]) )%5==0){
			    arr[i]="FizzBuzz";
			  }
		  }
		  return arr;
		}

Open in new window


above fails below tests
Expected      Run            
fizzBuzz(1, 6) → ["1", "2", "Fizz", "4", "Buzz"]      [null, "1", "2", "Fizz", "4"]      X      
fizzBuzz(1, 8) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      [null, "1", "2", "Fizz", "4", "Buzz", "Fizz"]      X      
fizzBuzz(1, 11) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      [null, "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz"]      X      
fizzBuzz(1, 16) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      [null, "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14"]      X      
fizzBuzz(1, 4) → ["1", "2", "Fizz"]      [null, "1", "2"]      X      
fizzBuzz(1, 2) → ["1"]      [null]      X      
fizzBuzz(50, 56) → ["Buzz", "Fizz", "52", "53", "Fizz", "Buzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 50 (line number:4)      X      
fizzBuzz(15, 17) → ["FizzBuzz", "16"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 15 (line number:4)      X      
fizzBuzz(30, 36) → ["FizzBuzz", "31", "32", "Fizz", "34", "Buzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 30 (line number:4)      X      
fizzBuzz(1000, 1006) → ["Buzz", "1001", "Fizz", "1003", "1004", "FizzBuzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1000 (line number:4)      X      
fizzBuzz(99, 102) → ["Fizz", "Buzz", "101"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 99 (line number:4)      X      
fizzBuzz(14, 20) → ["14", "FizzBuzz", "16", "17", "Fizz", "19"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 14 (line number:4)      X      
other tests
X      
Your progress graph for this problem
import java.util.Arrays;

public class FizzBuzzChallenge {
	public static void main(String[] args) {
		System.out.println("value is-->"+Arrays.toString(fizzBuzz(1, 6)));
	}

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

}

Open in new window

value is-->[null, 1, 2, Fizz, 4]
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 100

Assisted Solution

by:mlmcc
mlmcc earned 250 total points
ID: 41756762
I am not a Java programmer but in some languages arrays are indexed from 0.
If Java is such you need to account for it.

WHen you run for 50 to 56 the array is not indexed with 50, 51, etc.  You have to adjust the index or the values entered.

mlmcc
0
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 41758099
I found couple of logical issues and corrected the code with comments.

public String[] fizzBuzz(int start, int end) {
		  String [] arr=new String[(end-start)];
		  
		  for(int n=start;n<end;n++){
		    int i = n-start; // Calculation of number was missing
		    arr[i]=String.valueOf(start+i);
		  
		  // Corrected the condition order
    		 if( (Integer.parseInt(arr[i]) )%3==0  && (Integer.parseInt(arr[i]) )%5==0){
    			    arr[i]="FizzBuzz";
    		 } else if((Integer.parseInt(arr[i]) )%5==0){
    		    arr[i]="Buzz";
    		 } else if(  (Integer.parseInt(arr[i]) ) %3==0){
    		    arr[i]="Fizz";
    		 }
		  }
		  return arr;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41758130
 int i = n-start; // Calculation of number was missing

why we need above step? which number?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758413
public String[] fizzBuzz(int start, int end) {
		int len =end-start;
		  String [] arr=new String[(len)];
		  for(int k=start;k<(end);k++){
		    arr[k-1]=String.valueOf(k);
		  }
		  for(int i=0;i<(len);i++){ 
		     if( (Integer.parseInt(arr[i]) )%3==0  && (Integer.parseInt(arr[i]) )%5==0){
			    arr[i]="FizzBuzz";
			  }
		 else if(  (Integer.parseInt(arr[i]) ) %3==0){
		    arr[i]="Fizz";
		  }
		 else if((Integer.parseInt(arr[i]) )%5==0){
		    arr[i]="Buzz";
		  }
		  }
		  return arr;
		}

Open in new window


above fails bottom tests which do not start with 1
Expected      Run            
fizzBuzz(1, 6) → ["1", "2", "Fizz", "4", "Buzz"]      ["1", "2", "Fizz", "4", "Buzz"]      OK      
fizzBuzz(1, 8) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      OK      
fizzBuzz(1, 11) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      OK      
fizzBuzz(1, 16) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      OK      
fizzBuzz(1, 4) → ["1", "2", "Fizz"]      ["1", "2", "Fizz"]      OK      
fizzBuzz(1, 2) → ["1"]      ["1"]      OK      
fizzBuzz(50, 56) → ["Buzz", "Fizz", "52", "53", "Fizz", "Buzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 49 (line number:5)      X      
fizzBuzz(15, 17) → ["FizzBuzz", "16"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 14 (line number:5)      X      
fizzBuzz(30, 36) → ["FizzBuzz", "31", "32", "Fizz", "34", "Buzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 29 (line number:5)      X      
fizzBuzz(1000, 1006) → ["Buzz", "1001", "Fizz", "1003", "1004", "FizzBuzz"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 999 (line number:5)      X      
fizzBuzz(99, 102) → ["Fizz", "Buzz", "101"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 98 (line number:5)      X      
fizzBuzz(14, 20) → ["14", "FizzBuzz", "16", "17", "Fizz", "19"]      Exception:java.lang.ArrayIndexOutOfBoundsException: 13 (line number:5)      X      
other tests
X      
Your progress graph for this problem
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758418
public String[] fizzBuzz(int start, int end) {
		int len =end-start;
		  String [] arr=new String[(len)];
		  for(int k=start;k<(end);k++){
		    arr[k-start]=String.valueOf(k);
		  }
		  for(int i=0;i<(len);i++){ 
		     if( (Integer.parseInt(arr[i]) )%3==0  && (Integer.parseInt(arr[i]) )%5==0){
			    arr[i]="FizzBuzz";
			  }
		 else if(  (Integer.parseInt(arr[i]) ) %3==0){
		    arr[i]="Fizz";
		  }
		 else if((Integer.parseInt(arr[i]) )%5==0){
		    arr[i]="Buzz";
		  }
		  }
		  return arr;
		}

Open in new window


i  fixed above issue passign all tests. any modifications/improvements/refinements to above code?
Expected      Run            
fizzBuzz(1, 6) → ["1", "2", "Fizz", "4", "Buzz"]      ["1", "2", "Fizz", "4", "Buzz"]      OK      
fizzBuzz(1, 8) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7"]      OK      
fizzBuzz(1, 11) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"]      OK      
fizzBuzz(1, 16) → ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]      OK      
fizzBuzz(1, 4) → ["1", "2", "Fizz"]      ["1", "2", "Fizz"]      OK      
fizzBuzz(1, 2) → ["1"]      ["1"]      OK      
fizzBuzz(50, 56) → ["Buzz", "Fizz", "52", "53", "Fizz", "Buzz"]      ["Buzz", "Fizz", "52", "53", "Fizz", "Buzz"]      OK      
fizzBuzz(15, 17) → ["FizzBuzz", "16"]      ["FizzBuzz", "16"]      OK      
fizzBuzz(30, 36) → ["FizzBuzz", "31", "32", "Fizz", "34", "Buzz"]      ["FizzBuzz", "31", "32", "Fizz", "34", "Buzz"]      OK      
fizzBuzz(1000, 1006) → ["Buzz", "1001", "Fizz", "1003", "1004", "FizzBuzz"]      ["Buzz", "1001", "Fizz", "1003", "1004", "FizzBuzz"]      OK      
fizzBuzz(99, 102) → ["Fizz", "Buzz", "101"]      ["Fizz", "Buzz", "101"]      OK      
fizzBuzz(14, 20) → ["14", "FizzBuzz", "16", "17", "Fizz", "19"]      ["14", "FizzBuzz", "16", "17", "Fizz", "19"]      OK      
other tests
OK
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now