Amazon Interview Question for SDE1s


Country: India




Comment hidden because of low score. Click to expand.
1
of 1 vote

In python.

#!/usr/bin/env python

def main():
        str_15 = ("", "", "Fizz", "", "Buzz", "Fizz", "", "", "Fizz", "Buzz", "", "Fizz", "", "", "FizzBuzz")
        str_index = 0

        for i in range(1, 101):
                if str_15[str_index] == "":
                        print(i)
                else:
                        print(str_15[str_index])
                str_index += 1
                if str_index == 15:
                        str_index = 0

main()

- embeddedlinux April 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
        }
    }

- Shiva December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);


}
}

- ritz December 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}
}

- samokryl December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/local/bin/python
import fileinput

for line in fileinput.input():
    num = int(line)
    if num%3 == 0:
        print "fizz",
    if num%5 == 0:
        print "buzz",
    if num%3 != 0 and num%5 != 0:
        print num,
    print

- thushw December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complexity: One for loop means O(n) time complexity and no array needed means O(1) space complexity.
But,
Warning:
Try initiate a class where you only need static methods can give a very bad impression on your java proficiency.

- gameboy1024 December 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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).

- Felipe Heidrich December 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I just stop a mistake in my code above, when count5==0, reset count5 = 5...

- Felipe Heidrich December 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't it print 5Buzz for i==5? Also, it won't print for i==N.

- thushw December 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Felipe Heidrich December 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

FizzBuzz is to weed out bad candidates and they expect you to get it right pretty quickly.

Making even tiny mistakes on this question can be deterimental to your chances.

- Anonymous December 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
        }
    }

- readma December 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud one to have 15 in condition instead of checking indvidually 3 & 5.

- ashu December 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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..");

}

- ashu December 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- Anonim December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void main()
{
int a,n;
clrscr();
printf("enter n");
scanf("%d",&n);
for(a<n;a*3=0;a++)
{
for(a<n;a*5=0;a++)
{
if(a*3=0&&a*5=0)
printf("FIZZBUZZ");
else
printf("a");
}
printf("BUZz");
}
printf("FUZZ");
}

- vinni December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for (int i = 1; i <= n; i++)
{
bool flag = false;
if (i % 3 == 0)
{
Console.Write("Fizz");
flag = true;
}
if (i % 5 == 0)
{
Console.Write("Buzz");
flag = true;
}
else if(!flag)
{
Console.Write(i);
}
Console.WriteLine();
}

- Anonymous December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for (int i = 1; i <= n; i++)
{
bool flag = false;
if (i % 3 == 0)
{
Console.Write("Fizz");
flag = true;
}
if (i % 5 == 0)
{
Console.Write("Buzz");
flag = true;
}
else if(!flag)
{
Console.Write(i);
}
Console.WriteLine();
}

- Anonymous December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//javascript

function print(x) {
    document.body.innerHTML += "<p>"+x+"</p>"
}

function solution(number) {
    for(var i=1; i<=number; i++) {
        if(i%15==0) {
            print("FizzBuzz")
        }
        else if(i%3 == 0) {
            print("Fizz")
        } 
        else if(i%5==0) {
            print("Buzz")
        }
        else {
            print(i)
        }
        
        
    }
}

solution(15)

- Paul December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Nasim December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{ 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);

}

}

}}

- Anonymous December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

	}

}

- Anonymous December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
    }
}

- novice March 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- gonzaw308 October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- gonzaw308 December 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- pushpendra July 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

fuck

- dfd August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anybody help me to solve the below question ????
Sample Input:------------
2
3 15
Sample Output:----------
1
2
Fizz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

- Abhay Bachhar May 13, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More