## 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');
}
}``````

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

Comment hidden because of low score. Click to expand.
0

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

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

Comment hidden because of low score. Click to expand.
0

where is the case of "FooBar" ?

Comment hidden because of low score. Click to expand.
0

It is 'if' not 'if else'

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

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

My husband is at it again

ripsid is my hubs

Comment hidden because of low score. Click to expand.
0

You two bit hoare.

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.

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

Comment hidden because of low score. Click to expand.
0

I think there should be some clever way other than this

Comment hidden because of low score. Click to expand.
0

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

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

how about n is negative number

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

Seriously? Microsoft really asked this question?

Comment hidden because of low score. Click to expand.
0

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

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

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

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``````

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

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

This was asked in Philips too ..

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

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

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.

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

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

}

}``````

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

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

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

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.

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