Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
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");
}
}
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);
}
}
}
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
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);
}
}
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()));
}
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.
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;
}
#include <stdio.h>
#include <iostream>
int main()
{
for(int i = 1; i < 201; ++i)
{
const int isFoo = (i%3 == 0);
const int isBar = (i%5 == 0);
if (isFoo + isBar)
{
const int start = !isFoo * 3;
const int end = (isFoo + isBar) * 3;
std::cout << std::string_view{"FooBar" + start, end};
}
else
{
std::cout << i;
}
std::cout << "\n";
}
return 0;
}
#include <stdio.h>
#include <iostream>
int main()
{
for(int i = 1; i < 201; ++i)
{
const int isFoo = (i%3 == 0);
const int isBar = (i%5 == 0);
if (isFoo + isBar)
{
const int start = !isFoo * 3;
const int end = (isFoo + isBar) * 3;
std::cout << std::string_view{"FooBar" + start, end};
}
else
{
std::cout << i;
}
std::cout << "\n";
}
return 0;
}
Why not add two variables to save the results of the mod operations so as to save doing them once more?
If we don't use the mod operation, will it be faster?
- uuuouou December 01, 2013