Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Why not add two variables to save the results of the mod operations so as to save doing them once more?

void FooBar(int n){
        int i, r3, r5;
        for(i = 1; i <= n; ++i){
            r3 = i % 3;
            r5 = i % 5;
            if(r3 && r5) printf("%d", i);
            else{
                if(!r3) printf("Foo");
                if(!r5) printf("Bar");
            }
            putchar('\n');
        }
    }

If we don't use the mod operation, will it be faster?

void FooBar(int n){
        int i1 = 1, i3 = 3, i5 = 5;
        for(; i1 <= n; ++i1){
            if(i1 != i3 && i1 != i5) printf("%d", i1);
            else{
                if(i1 == i3){
                    printf("Foo");
                    i3 += 3;
                }
                if(i1 == i5){
                    printf("Bar");
                    i5 += 5;
                }
            }
            putchar('\n');
        }
    }

- uuuouou December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Brilliant :) I never thought such an easy question would have an elegant answer

- chid1989 January 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Second solution does not cover the case of a number being divisible by both 3 and 5 right!

- Sailakshmi June 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void FooBar(int n)
{
	for( int i=1;  i <= n;  i++, print("\n") )
	{
		if( i%3 && i%5)	 print( i ), continue;
		if( i % 3 ==0) 	 print( "Foo");
		if( i % 5 ==0)  	 print( "Bar");
	}
}

- = November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

where is the case of "FooBar" ?

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

It is 'if' not 'if else'

- Sugarcane_farmer May 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void FooBar(int n){
		for(int i=1;i<=n;i++){
			
			if(i%5!=0 && i%3!=0){
				System.out.println(i);
				continue;
			}
			if(i%3==0){
				System.out.print("Foo");
			}
			if(i%5==0){
				System.out.print("Bar");
			}
			System.out.println();
		}
	}

- rpisid November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My husband is at it again

ripsid is my hubs

- Subbu's wife November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You two bit hoare.

- Subbu November 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Typical of microsoft. Copy others and give their own boring name.

Fizz -> Foo
Buzz -> Bar.

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

public void fooBar(int n) {
	for (int i = 1; i <= n; i++) {
	    if (i % 3 == 0 && i % 5 == 0) {
		System.out.println("FooBar");
	    } else if (i % 3 == 0) {
		System.out.println("Foo");
	    } else if (i % 5 == 0) {
		System.out.println("Bar");
	    } else {
		System.out.println(i);
	    }
	}
    }

- Anonymous November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think there should be some clever way other than this

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

In above code.say my n= 20 and when the value of i= 15; it will go into first if statement and print FooBar but will it also execute other if statements as well to print foo and Bar. Can I use break; in first if ststement if I found FooBar there only. so that it will come out mmediately

- megh August 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about n is negative number

- tt November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seriously? Microsoft really asked this question?

- Anonymous November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

may be ...they wanted to know how a person might solve this without using mod and minimum calls to out stream .

- devikiranr April 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void FooBar(int n){
		
		for(int i=1;i<=n;i++){
			if(i%3==0 && i%5==0){System.out.println("FooBar");}
			else if(i%3==0){System.out.println("Foo");}
			else if(i%5==0){System.out.println("Bar");}
			else{System.out.println(i);}
		}
	}

- rohitgarg.alwar November 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void FooBar(int n){

for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){System.out.println("FooBar");}
else if(i%3==0){System.out.println("Foo");}
else if(i%5==0){System.out.println("Bar");}
else{System.out.println(i);}
}
}

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

F#:

let rec FooBar(n : int) = 
    let InnerCall x txt = 
        printf "%A\n" txt
        FooBar(x - 1)
    match n with
    | 0 -> printf ""
    | x when x % 15 = 0 -> InnerCall x "FooBar"
    | x when x % 3 = 0 -> InnerCall x "Foo"
    | x when x % 5 = 0 -> InnerCall x "Bar"
    | x -> InnerCall x x

- sattar.imamov January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string temp = string.Empty;
for (int i = 1; i <=15; i++)
{
temp= (i % 3 == 0 && i % 5 == 0)?"Foo Bar":(i%5==0?"Bar":(i%3==0?"Foo":Convert.ToString(i)));
lstBoxGetName.Items.Add(temp);
}

- searcher January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This was asked in Philips too ..

- swathi sathya prakash January 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FooBar {

	public static void fooBar(int num){
		int i1 = 1, i3 = 3, i5 = 5, i15 = 15;
		for(; i1 <= num+1; i1++){		 
			if(i1 == i15){
				System.out.println(i1+" FooBar");
				i15 += 15;
				i3 += 3;
				i5 += 5;
			}
			else if(i1 == i3){
				System.out.println(i1+" Foo");
				i3 += 3;
			}
			else if(i1 == i5){
				System.out.println(i1+" Bar");
				i5 += 5;
			}
		}
	}

	public static void main(String[] args){
		fooBar(75);
	}
}

- Milky September 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You know you can nest if statements, right?

public static void FooBar(int n)
        {
            for(int i = 1; i <= n; i++)
            {
                if(i % 3 == 0)
                {
                    if(i % 5 == 0)
                    {
                        Console.WriteLine("FooBar");
                    }
                    else
                    {
                        Console.WriteLine("Foo");
                    }
                }
                else if(i % 5 == 0)
                {
                    Console.WriteLine("Foo");
                }
                else
                {
                    Console.WriteLine(i);
                }
            }
        }

Same version, condensed:

public static void FooBar(int n)
        {
            for(int i = 1; i <= n; i++)
                Console.WriteLine(i % 3 == 0 ? (i % 5 == 0 ? "FooBar" : "Foo") : (i % 5 == 0 ? "Bar" : i.ToString()));
        }

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

If I were the interviewer, I would tell you that trying to optimize the int arithmetics in the loop is ridiculous vs the cost of the print functions, so no need to bother, just wasting time for nothing.
1st aim of the writing style should be to keep it concise and understandable at first glance. I notice none of the solutions include any comment.
So to me this is a no-pass.

- Greg May 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Minified JavaScript version:

FooBar=(n)=>{for(i=0;i++<n;)console.log(((i%3?"":"Foo")+(i%5?"":"Bar"))||i)};

- Stefan Berkner December 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function FooBar(n){

var r3, r5;

for(var i = 1; i <= n; i++){

r3 = i % 3;
r5 = i % 5;

if(r3 != 0 && r5 !=0){
    console.log(i + '\n');
}

if(r3 == 0 && r5 == 0){
    console.log('FooBar \n');
}else if(i % 3 == 0){
    console.log('Foo \n');
} else if(i % 5 == 0){
    console.log('Bar \n');
}

}

}

- Mike July 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void fooBar (int n){
    for (int i=1; i<=n; i++){
         System.out.println((i%3==0?(i%5==0?"FooBar":"Foo"):(i%5==0?"Bar":i)));
    }
}

- Sindhu Venkatachary December 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let arr = [];
for(let i=1;i<=100;i++){
  arr.push(i)
};
arr.forEach(item=> {
  return (item%5===0 && item%3===0) ? console.log('foobar') 
  : item%5===0 ? console.log('bar') 
  : item%3===0 ? console.log('foo') 
  : console.log(item)
})

- Michael September 07, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static String FOO = "Foo";
    private static String BAR = "Bar";

    private void fooBarUpTo(int n){
        for(int i = 1; i <= n; i++) System.out.println(fooBar(i));
    }

    private String fooBar(int i) {
        if (i % 15 == 0) return FOO + BAR;
        if (i % 3 == 0) return FOO;
        if (i % 5 == 0) return BAR;
        return "" + i;
    }

- Bram Dooddejong August 01, 2020 | 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