Amazon Interview Question
SDE1sCountry: India
public static IEnumerable<object> FizzBuzzCovertor(int inputNo)
{
var result = new List<object>();
for (int i = 1; i < inputNo + 1; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
result.Add("FizzBuzz");
yield return result;
}
else
{
if (i % 3 == 0)
{
result.Add("Fizz");
i++;
yield return result;
}
if (i % 5 == 0)
{
result.Add("Buzz");
i++;
yield return result;
}
}
result.Add(i);
}
yield return result;
}
}
public class Fizz_Buzz{
public void getTheNumberDetails(int num){
for(int i=1;i<=num;i++){
int s=3*5;
if(i%3==0 && i%5==0){
System.out.println("FizzBuzz");
}
else if(i%3==0){
System.out.println("Fizz");
}
else if(i%5==0){
System.out.println("Buzz");
}
else{
System.out.println(i);
}
}
}
public static void main(String args[]){
Fizz_Buzz fb= new Fizz_Buzz();
fb.getTheNumberDetails(15);
}
}
import java.util.Scanner;
public class NumSequenceTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number = in.nextInt();
for(int i = 1; i<number+1; i++){
String tempStr="";
if(checkMultiples3(i)){
tempStr+="Fizz";
}
if(checkMultiples5(i)){
tempStr+="Buzz";
}
if(tempStr.isEmpty()){
tempStr=String.valueOf(i);
}
System.out.println(tempStr);
}
}
static boolean checkMultiples3(int n){
if(n%3==0)
return true;
return false;
}
static boolean checkMultiples5(int n){
if(n%5==0)
return true;
return false;
}
}
Personally I would avoid any solution using a mod operation (2 in the case).
Also, when analyzing N one should try to use and relevant info from the previous iterations.
Instead of using mod I would have 2 counters:
int count3 = 3;
int count5 = 5;
for (int i = 1; i<N; i++) {
String s = i+"";
count3--;
if (count3==0) {
count3=3;
s = "Fizz";
}
count5--;
if (count5==0) {
count5=0;
s += "Buzz"
}
System.out.println(s);
}
Incrementing counters are far cheaper than using mod 3 and mod 5 for each i.
to handle i=0 one should start counter3 and counter5 to zero (didn't test, but I think it would work fine).
Right, easy fix. Here is a modified version that gets it right:
static void fizzBuzz(final int N, PrintStream out) {
int count3 = 3;
int count5 = 5;
for (int i = 1; i <= N; i++) {
String s = "";
if (count3 == i) {
count3 += 3;
s += "Fizz";
}
if (count5 == i) {
count5 += 5;
s += "Buzz";
}
if (s.isEmpty()) {
s = String.valueOf(i);
}
if (out != null) out.println(s);
}
}
That said, it takes the same time as the version using mod.
Next idea in the list is to use lambda, if out-of-order is okay then we can use the
mod version to run it in parallel.
static String FizzBuzzMap(int i) {
String tempStr="";
if(i%3==0){
tempStr+="Fizz";
}
if(i%5==0){
tempStr+="Buzz";
}
if (tempStr.isEmpty()){
tempStr=String.valueOf(i);
}
return tempStr;
}
static void fizzBuzzLambda(final int N, PrintStream out) {
IntStream.range(1, N+1)
.parallel()
.mapToObj(Main::FizzBuzzMap)
.forEach(i -> out.println(i));
}
The lambda version (using parallel) runs 5x faster on a intel core i7.
for (int i=1; i<=N; i++)
{
if (i % 15 == 0)
{
printf("FizzBuzz\n");
}
else if (i % 3 == 0)
{
printf("Fizz\n");
}
else if (i % 5 == 0)
{
printf("Buzz\n");
}
else
{
printf("%d\n", i);
}
}
private static void method1()
{
Console.Write("Enter the no. : ");
Int16 num = Convert.ToInt16(Console.ReadLine());
if (num >= 3)
{
for (Int16 i = 1; i < num + 1; i++)
{
if (i % 3 == 0 && i % 5 == 0) Console.WriteLine("FizzBuzz");
else if (i % 3 == 0) Console.WriteLine("Fizz");
else if (i % 5 == 0) Console.WriteLine("Buzz");
else Console.WriteLine(i);
}
}
else Console.WriteLine("enter a valid no..");
}
#include <iostream>
using namespace std;
int main()
{
int number = 25;
int index = 0;
bool enter = false;
do
{
if (index % 3 == 0 && index>0)
{
cout << "Fizz";
enter = true;
}
if (index % 5 == 0 && index>0)
{
cout << "Buzz";
enter = true;
}
if (!enter)
{
cout << index;
}
cout << "\n";
enter = false;
} while (++index <= number);
system("pause");
return 0;
}
Simple Solution:
public void printNumbers(int num){
for (int i =1;i<=num;i++){
int flag = 0;
if ( i % 3 == 0 ){
System.out.print("Fizz");
flag = 1;
}
if (i % 5 == 0){
System.out.print("Buzz");
flag =1;
}
if (flag == 0){
System.out.print(i);
}
System.out.print("\n");
}
}//End printNumbers
{{ import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FizzBuzz {
/**
* @param args
*/
void print(Map<Integer, String> m, int n){
StringBuilder b = new StringBuilder();
boolean flag ;
for(int i=1;i<=n;i++){
flag = false;
for(Entry<Integer, String> s : m.entrySet()){
if(i%s.getKey()==0){
b.append(s.getValue());
flag = true;
}
}
if(!flag)
b.append(i);
b.append(" ");
}
System.out.println(b);
}
public static void main(String[] args) {
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(3, "Fizz");
m.put(5, "Buzz");
m.put(7, "kabootar");
new FizzBuzz().print(m,105);
}
}
}}
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class FizzBuzz {
/**
* @param args
*/
void print(Map<Integer, String> m, int n){
StringBuilder b = new StringBuilder();
boolean flag ;
for(int i=1;i<=n;i++){
flag = false;
for(Entry<Integer, String> s : m.entrySet()){
if(i%s.getKey()==0){
b.append(s.getValue());
flag = true;
}
}
if(!flag)
b.append(i);
b.append(" ");
}
System.out.println(b);
}
public static void main(String[] args) {
Map<Integer, String> m = new HashMap<Integer, String>();
m.put(3, "Fizz");
m.put(5, "Buzz");
m.put(7, "kabootar");
new FizzBuzz().print(m,105);
}
}
public class MultiplesMain {
public static List<String> fizzBuzz(int start, int end, Map<Integer, String> lookups) {
List<String> out = new ArrayList<String>();
for(int i=start; i <=end; i++) {
String str = i+":";
for(Map.Entry<Integer, String> entry :lookups.entrySet()) {
if(isMultiOf(i, entry.getKey())) {
str+=entry.getValue();
}
}
out.add(str);
}
return out;
}
public static boolean isMultiOf(int n, int m) {
return (n % m == 0);
}
public static void main(String[] args) {
Map<Integer, String> lookups = new HashMap<Integer, String>();
lookups.put(3, "Fizz");
lookups.put(5, "Buzz");
lookups.put(7, "Woof");
List<String> output = fizzBuzz(-1, 50, lookups);
System.out.println(output);
}
}
import Data.Monoid
import Control.Monad
-- Generic fizzbuzzer, for any monoid, for any list of possible fizzers or
-- buzzers, and for any function that
-- transforms the integer to a monadic value
fizzbuzzer :: Monoid a => [(Int,a)] -> (Int -> a ) -> Int -> a
fizzbuzzer printers trans n = app id (trans n)
where
app = appEndo . mconcat . fmap Endo . fmap test $ printers
test (val,str) f
| n `mod` val == 0 = const (str <> f mempty)
| otherwise = f
-- Specific fizzbuzz, takes a number and returns either that number as a
-- string, "Fizz", "Buzz" or "FizzBuzz"
fizzbuzz :: Int -> String
fizzbuzz = fizzbuzzer [(3,"Fizz"),(5,"Buzz")] show
-- IO action to get the N and print all fizzbuzzes up until that N
ioFizzBuzz n = forM_ [1 .. n] $ putStrLn . fizzbuzz
-- Gets number from STDIN and calls the IO FizzBuzz action
main = getLine >>= (ioFizzBuzz . read)
Found a simpler way in 3 lines of Haskell code:
import Data.Monoid
zzer (d, str) n = if n `mod` d == 0 then Just str else Nothing
functions = mconcat . map zzer
fizzbuzzer list n = maybe (show n) id (functions list n)
Example use:
printFizzBuzz n = mapM_ (putStrLn . fizzbuzzer [(3, "Fizz"), (5, "Buzz")]) $ [1..n]
main = printFizzBuzz 100
Type signatures:
zzer :: Integral a => (a,String) -> a -> Maybe String
functions :: Integral a => [(a,String)] -> a -> Maybe String
fizzbuzzer :: Integral a => [(a,String)] -> a -> String
can anybody write in cpp
Given an positive integer N, print all the integers from 1 to N. But for multiples of 3 print “Fizz” instead of the number and for the multiples of 5 print “Buzz”. Also for number which are multiple of 3 and 5, prints “FizzBuzz”.
Example
N = 5
Return: [1 2 Fizz 4 Buzz]
Note: Instead of printing the answer, you have to return it as list of strings.
In python.
- embeddedlinux April 16, 2016