Skip to main content

On This Page

Implement the FizzBuzz Puzzle in Java

3 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

Implement the FizzBuzz Puzzle in Java

The FizzBuzz puzzle, popularized as a coding interview question in 2007 by Imran Ghory, tests fundamental programming concepts like conditional logic and modular arithmetic. The problem involves iterating from 1 to n, printing “Fizz” for multiples of 3, “Buzz” for multiples of 5, and “FizzBuzz” for multiples of both.

Why This Matters

While seemingly trivial, FizzBuzz reveals a candidate’s ability to translate a problem statement into functional code. Inefficient or overly complex solutions can indicate a lack of understanding of basic operators and control flow, leading to maintainability issues in larger systems – a seemingly small error can scale to impact critical application logic.

Key Insights

  • FizzBuzz as an Interview Tool: First used extensively as a Google interview question, it quickly spread throughout the industry.
  • Modulo Operator (%): The core logic relies on the modulo operator to determine divisibility, impacting performance for very large n.
  • StringBuilder for Efficiency: Methods utilizing StringBuilder outperform repeated string concatenation, especially within loops.

Working Example

import java.util.ArrayList;
import java.util.List;

public class FizzBuzz {

    public List<String> fizzBuzzNaive(int n) {
        List<String> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                result.add("FizzBuzz");
            } else if (i % 3 == 0) {
                result.add("Fizz");
            } else if (i % 5 == 0) {
                result.add("Buzz");
            } else {
                result.add(String.valueOf(i));
            }
        }
        return result;
    }

    public List<String> fizzBuzzConcatenation(int n) {
        List<String> result = new ArrayList<>();
        StringBuilder output = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0) {
                output.append("Fizz");
            }
            if (i % 5 == 0) {
                output.append("Buzz");
            }
            result.add(output.length() > 0 ? output.toString() : String.valueOf(i));
            output.setLength(0);
        }
        return result;
    }

    public List<String> fizzBuzzCounter(int n) {
        List<String> result = new ArrayList<>();
        StringBuilder output = new StringBuilder();
        int fizz = 0;
        int buzz = 0;
        for (int i = 1; i <= n; i++) {
            fizz++;
            buzz++;
            if (fizz == 3) {
                output.append("Fizz");
                fizz = 0;
            }
            if (buzz == 5) {
                output.append("Buzz");
                buzz = 0;
            }
            result.add(output.length() > 0 ? output.toString() : String.valueOf(i));
            output.setLength(0);
        }
        return result;
    }
}

Practical Applications

  • Code Quality Assessment: Used by companies like Google to quickly assess a candidate’s coding skills.
  • Pitfall: Overly complex solutions or incorrect handling of the “FizzBuzz” condition demonstrate poor attention to detail and logic.

References:

Continue reading

Next article

Introduction to MS Excel for Data Analytics

Related Content