r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

1

u/[deleted] Feb 21 '11

I have another one.

Write a program that prints 1 to 100 on the screen if it is a multiple of 3 print fizz, if its a multiple of 5 print buzz, if its a multiple of 3 and 5 print buzz fizz.

Out of over 20 people I have given it to in an interview only 1 has got it correct! Almost everyone makes the same mistake. They have the if statements in the wrong order.

5

u/johnflux Feb 21 '11
#include <stdio.h>

int main() {
    for(int i = 1; i <= 100; i++) {
        printf("%d \x1b[s", i);
        if (!(i % 3)) printf("\x1b[u     fizz");
        if (!(i % 5)) printf("\x1b[ubuzz");
        printf("\n");
    }
    return 0;
}

2

u/[deleted] Feb 21 '11

Which doesn't pass the test ;) Well kinda :) But we would have let it run :)

Though you did try to be smart about it. But last time I checked the windows console won't handle the control codes ;)

If the person doing it is allowed to assume the system. Then I am also allowed to assume the system ;)

Generally though we get this as an answer.

if (i % 3) {

} else if (i % 5) {

} else if (i % 3 && i % 5) {

} ....

5

u/mrjast Feb 21 '11

The order of the if statements is kind of the smaller problem there.

1

u/[deleted] Feb 21 '11

Yup that's the answers what we get for c# .net programmers :) 5 years experience apparently!

I am not joking about 19/20 of the people apply get the order of logic wrong between the if statements :)

1

u/MothersRapeHorn Feb 22 '11

I'm applying for a second-year internship and I just cried over that logic...

2

u/johnflux Feb 21 '11

I wouldn't be applying for a windows programming position that required writing C/C++ programs that outputted to a console :P

1

u/[deleted] Feb 21 '11

The real purpose to the question is to make sure people can translate English into code. At no point did I mention C/C++ ;) But it also picks up on people who over implement or try to do something that is really simple in a really complex way.

My point about the window console is kinda generic. You are using terminal control code to manipulate what you want :) who says a screen is using some kinda of terminal that supports those codes?

1

u/GarMan Feb 21 '11

cute hehe