Integers: Fizz Buzz interview problem

Problem stattement:

Soltuions:

1. Using If-Else Brute force

    private static String[] fizzBuzzBruteForce(int n) {
        String[] result = new String[n];

        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                result[i - 1] = "FizzBuzz";
            } else if (i % 3 == 0) {
                result[i - 1] = "Fizz";
            } else if (i % 5 == 0) {
                result[i - 1] = "Buzz";
            } else {
                result[i - 1] = i + "";
            }
        }
        return result;
    }

2. Using If-Else Simplified

     /**
     * Optimized Solution (Avoid Multiple Modulus Operations)
     * Instead of checking i % 3 and i % 5 multiple times, we can store the result in a StringBuilder:
     *
     * TC: O(N)
     * SC: O(N)
     * @param n max
     * @return string[]
     */
    private static String[] fizzBuzz_buzz_string_builder(int n) {
        String[] result = new String[n];

        for (int i = 1; i <= n; i++) {
            StringBuilder sb = new StringBuilder();
            if (i % 3 == 0) sb.append("Fizz");
            if (i % 5 == 0) sb.append("Buzz");

            // result[i - 1] = sb.length() > 0 ? sb.toString() : i + ""; //both works fines
            result[i - 1] = sb.isEmpty() ? i + "" : sb.toString();
        }
        return result;
    }

3. Enhance to add custom divisors rule

    /**
     * Fizz Buzz Bazz custom divisors
     *
     * @param n     input size
     * @param rules divisors
     */
    private static String[] fizzBuzzCustomDivisors(int n, Map<Integer, String> rules) {

        final String[] result = new String[n];

        for (int i = 1; i <= n; i++) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<Integer, String> entry : rules.entrySet()) {
                if (i % entry.getKey() == 0) {
                    sb.append(entry.getValue());
                }
            }
            //append to result array
            result[i - 1] = sb.isEmpty() ? i + "" : sb.toString();
        }

        return result;
    }

4. Implement solution using Java-Streams

    /**
     * Implement fizz buzz using java 8 streams.
     *
     * @param n
     * @param rules
     * @return
     */
    private static String[] fizzBuzzCustomDivisorsStreams(int n, Map<Integer, String> rules) {
        return IntStream.rangeClosed(1, n).mapToObj(i -> {

            String res = rules.entrySet().stream()
                    .filter(entry -> i % entry.getKey() == 0)
                    .map(Map.Entry::getValue)
                    .reduce("", String::concat);
            return res.isEmpty() ? i + "" : res;
        }).toArray(String[]::new);
    }

5. Implement solution using Java-Streams Parallelism

    /**
     * Implement fizz buzz using java 8 streams and parallelism.
     *
     * @param n
     * @param rules
     * @return
     */
    public static String[] fizzBuzzCustomDivisorsStreamsParallel(int n, Map<Integer, String> rules) {
        return IntStream.rangeClosed(1, n)
                .parallel() // Enables parallel execution
                .mapToObj(i -> {
                    String result = rules.entrySet().stream()
                            .filter(entry -> i % entry.getKey() == 0)
                            .map(Map.Entry::getValue)
                            .reduce("", String::concat);
                    return result.isEmpty() ? String.valueOf(i) : result; // FIXED: Use `i` instead of `N`
                })
                .toArray(String[]::new);
    }

------ End ------