I was recently given a coding test to complete for a potential client. It was a FizzBuzz-type of thing with a two-hour time limit. I had a request to write basic FizzBuzz, then add a special case, then add a report. I didn't get an interview, and they didn't return any feedback so I don't know why. Can you take a look at this and let me know where I went wrong? It all looks good to me.
Edit A lot of reviewers have questioned the duplication in my code. The coding-test called for me to write fizzbuzz, show the results, make a change, show the results, then make another change and show the results. This is why there are three methods -- to show the results of each part of the test. I usually wouldn't have duplication like this in my code. It's best to review each method in isolation, as if it's the only method in the code.
package com.engineerdollery;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.*;
public class FizzBuzz {
private static final String NUMBER = "\\d+";
public String basic() {
return IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(joining(" "));
}
public String lucky() {
return IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> Integer.toString(i).contains("3") ? "lucky" // this is the only change from basic()
: i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(joining(" "));
}
public String counter() {
List<String> fizzBuzzList = IntStream.rangeClosed(1, 20)
.parallel()
.mapToObj(i -> Integer.toString(i).contains("3") ? "lucky"
: i % 15 == 0 ? "fizzbuzz"
: i % 3 == 0 ? "fizz"
: i % 5 == 0 ? "buzz"
: Integer.toString(i))
.collect(Collectors.toList());
Map<String, Long> countMap = fizzBuzzList
.parallelStream()
.collect(groupingBy(s -> s.matches(NUMBER) ? "integer" : s, counting()));
// reports
String fizzbuzz = fizzBuzzList.parallelStream().collect(joining(" "));
String counts = countMap.entrySet().parallelStream()
.map(e -> e.getKey() + ": " + e.getValue())
.collect(joining("\n"));
return fizzbuzz + "\n" + counts;
}
}
Test
package com.engineerdollery;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class FizzBuzzTest {
public static void main(String[] args) {
System.out.println(new FizzBuzz().counter());
}
@Test
public void basic() {
FizzBuzz fizzBuzz = new FizzBuzz();
String actual = fizzBuzz.basic();
assertEquals("1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 17 fizz 19 buzz", actual);
}
@Test
public void lucky() {
FizzBuzz fizzBuzz = new FizzBuzz();
String actual = fizzBuzz.lucky();
assertEquals("1 2 lucky 4 buzz fizz 7 8 fizz buzz 11 fizz lucky 14 fizzbuzz 16 17 fizz 19 buzz", actual);
}
@Test
public void counter() {
FizzBuzz fizzBuzz = new FizzBuzz();
String actual = fizzBuzz.counter();
assertTrue(actual.contains("1 2 lucky 4 buzz fizz 7 8 fizz buzz 11 fizz lucky 14 fizzbuzz 16 17 fizz 19 buzz"));
assertTrue(actual.contains("fizz: 4"));
assertTrue(actual.contains("buzz: 3"));
assertTrue(actual.contains("fizzbuzz: 1"));
assertTrue(actual.contains("lucky: 2"));
assertTrue(actual.contains("integer: 10"));
}
}