Foo - Less than 1% of CS grads can do this...

Bikeforums.net is a forum about nothing but bikes. Our community can help you find information about hard-to-find and localized information like bicycle tours, specialties like where in your area to have your recumbent bike serviced, or what are the best bicycle tires and seats for the activities you use your bike for.




Pages : [1] 2

mlts22
09-16-08, 12:28 AM
Supposedly, and the sources really can't be verified so take it for what it is worth, almost no CS grads can write a program to do the following:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.


mattm
09-16-08, 12:31 AM
i wish interview questions were that easy!

iamlucky13
09-16-08, 12:44 AM
Is there something more to it than a couple while statements?


annc
09-16-08, 12:54 AM
Less than a couple while loops...

DannoXYZ
09-16-08, 01:40 AM
Heck, I can do that with a single pass through one while-loop...

jschen
09-16-08, 01:44 AM
Heck... with no need to input a range, the program can just spit out a predefined string if the programmer wants a brute force solution!

keithm0
09-16-08, 01:55 AM
i wish interview questions were that easy!

Sadly, I've seen interview candidates fail even simpler questions.

mattm
09-16-08, 02:36 AM
Sadly, I've seen interview candidates fail even simpler questions.

like what?

crtreedude
09-16-08, 03:05 AM
Yeah, I know they are that bad... I used to interview them.

My test were worse. I would print out code and ask them.

"What does this code do?" after removing the obvious clues from function names.

You could see the beads of sweat appear...

Made sense to me. If I appear infront of a Latin American claiming I know Spanish, I better be ready for them to switch into Spanish.

MrCrassic
09-16-08, 03:43 AM
C++: You only need a
for statement to complete this, and a couple of checks to see whether the number modulo 3 or 5 is zero...



for (int i = 1; i <= 100; i++)
{
if (i%3==0)
cout << "Bizz!";
if (i%5==0)
cout << "Buzz!";
if (i%3==0&&i%5==0) //15 is multiple of both
cout << "Bizz buzz!";
else
cout << "Number: " << i;
**


And I'm in Computer Engineering...?

ehidle
09-16-08, 03:52 AM
C++: You only need a
for statement to complete this, and a couple of checks to see whether the number modulo 3 or 5 is zero...



for (int i = 1; i <= 100; i++)
{
if (i%3==0)
cout << "Bizz!";
if (i%5==0)
cout << "Buzz!";
if (i%3==0&&i%5==0) //15 is multiple of both
cout << "Bizz buzz!";
else
cout << "Number: " << i;
**


And I'm in Computer Engineering...?

So, your code here will output "Bizz!" for multiples of three, "Buzz!" for multiples of 5, and "Bizz!Buzz!Bizz buzz!" for numbers that are multiples of both 3 and 5...

You need to exclude multples of 5 from the first if statement and multiples of 3 from the second if statement and multples of both from both the first and second if statements.

crtreedude
09-16-08, 04:02 AM
So, your code here will output "Bizz!" for multiples of three, "Buzz!" for multiples of 5, and "Bizz!Buzz!Bizz buzz!" for numbers that are multiples of both 3 and 5...

You need to exclude multples of 5 from the first if statement and multiples of 3 from the second if statement and multples of both from both the first and second if statements.

Actually I would flunk you all, you missed the key component. The spec is vague - the output can be either with multiple outputs or not. This should be resolved first.

One of the most basic skills is know when a spec is not complete.

But, you are correct, MrCrassic would fail by either interpretation, because the number would be printed out unless the the number was multiples of 3 and 5. The instruction clearly states that the number should only be printed out in the case of no match on the conditions.

MrCrassic
09-16-08, 04:12 AM
So, your code here will output "Bizz!" for multiples of three, "Buzz!" for multiples of 5, and "Bizz!Buzz!Bizz buzz!" for numbers that are multiples of both 3 and 5...

You need to exclude multples of 5 from the first if statement and multiples of 3 from the second if statement and multples of both from both the first and second if statements.

So just remove the last IF statement?

crtreedude
09-16-08, 04:12 AM
for(int i = 1; i++ ; i <= 100)
{
if(i % 3 == 0) printf("fizz");
if(i % 5 == 0) printf("buzz");
if(i % 3 && i % 5) printf("%d",i);
printf("\n);
**

I am sure I could reduce this, but this is quick and dirty. No need to make a special case out of 5 and 3 together since you can just join them. Not tested, I have no compiler on this computer.

The benefits to test like this is they help you see how the person thinks since there are many, many ways to solve this.

crtreedude
09-16-08, 04:14 AM
So just remove the last IF statement?

You need to have an else between each IF statement, not just the final one, if we are too assume that a single line of output is desired for each iterative.

crtreedude
09-16-08, 04:18 AM
By the way, if I was coding for speed of execution, I would write it completely different. printfs are expensive to execute, so I would build the string before printing - the code would look much weirder and harder to follow.

I used to write DSP code where everything has to be super tight. When an eternity is 20 ms, you learn to be efficient...

ehidle
09-16-08, 04:51 AM
I used to write DSP code where everything has to be super tight. When an eternity is 20 ms, you learn to be efficient...

I used to write code to do numerical electromagnetic analysis. Think solving million x million matrices. A single simulation might take a couple of weeks to run, so every millisecond that could be shaved from a routine could save hours of time off of a simulation.

Now I work on embedded control systems with milliseconds control resolution. Everything has to be super tight - no wasted instructions.

And I'm a hardware guy... hah..

Jerseysbest
09-16-08, 05:33 AM
I barely know how code and could do that.

I guess CS grads do more theoretical work instead of practical?

Indy_Rider
09-16-08, 05:45 AM
That is probably because 99% of CS majors would ask why and tell them that is the dumbest question they have ever heard. People mistake not wanting to waste their time with a really freaking stupid question with not being able to do it.

hos13
09-16-08, 07:55 AM
This is so easy, I could do it with a Turing Machine. :D (Not really, But I did have to add 2 numbers together once with a Turing Machine)

MrCrassic
09-16-08, 08:07 AM
Actually I would flunk you all, you missed the key component. The spec is vague - the output can be either with multiple outputs or not. This should be resolved first.

One of the most basic skills is know when a spec is not complete.

But, you are correct, MrCrassic would fail by either interpretation, because the number would be printed out unless the the number was multiples of 3 and 5. The instruction clearly states that the number should only be printed out in the case of no match on the conditions.

Forgot the else statement...darn!



for (int i = 1; i <= 100; i++)
{
if (i%3==0)
cout << "Bizz!";
else if (i%5==0)
cout << "Buzz!";
else if (i%3==0&&i%5==0) //15 is multiple of both
cout << "Bizz buzz!";
else
cout << "Number: " << i;
**

MrCrassic
09-16-08, 08:09 AM
My only corporate software development experience was with working in a software division of a hedge fund. I did some Java GUI code (HORRORS!!!), and towards the end, I finally worked on some C++ backend server processes.

I like coding, but hated the environment, hence the reason why I'm trying business analysis right now (though I wish I could do some coding!)

black_box
09-16-08, 08:10 AM
i'm no efficiency expert, but couldnt you just use a string of some kind and concatenate or += the Buzz on modulo 5? you'd have to clear the variable at the end of each loop though.

KingTermite
09-16-08, 08:58 AM
Supposedly, and the sources really can't be verified so take it for what it is worth, almost no CS grads can write a program to do the following:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

That is so, so, so, so saaaad.

I remember a girl I was flirting with in college. I was Computer Engineering, she was Computer Science (close majors, CE = CS + a few extra classes). She got an intern job and she was telling me a story of how just to help her get started and understand the compiler they used, they asked her to write a "Hello World" program. She couldn't do it. :(

Besides all that, I just can't believe that less than 1% of CS majors could do this. That's like saying less than 1% of English majors couldn't identify the verb in a sentence.

mattm
09-16-08, 09:03 AM
now do it without using the mod operator..

MrCrassic
09-16-08, 09:11 AM
That is so, so, so, so saaaad.

I remember a girl I was flirting with in college. I was Computer Engineering, she was Computer Science (close majors, CE = CS + a few extra classes). She got an intern job and she was telling me a story of how just to help her get started and understand the compiler they used, they asked her to write a "Hello World" program. She couldn't do it. :(

Besides all that, I just can't believe that less than 1% of CS majors could do this. That's like saying less than 1% of English majors couldn't identify the verb in a sentence.

Here's the paradox about computer science:

It's advertised as a major that will assist you in learning how to develop software, but it really is the science of computing (which is MUCH more profound than the modern computer) and, to make matters worse, some schools place very little emphasis on actual software development! To make matters worse, employers see these students as potential assets for development instead of what (many) areare really trained for, which is a continuation in the study or an entry-level research position. Fortunately, at my school, a heavy dose of programming is included in their curriculum, along with the essentials that build a computer science graduate. It's no surprise, then, that there are many graduates who graduate with little working knowledge of how to code. In fact, if you take a visit to Slashdot and read up some Ask Slashdot threads, that is actually a common complaint amongst more seasoned developers and managers.

That is precisely the reason why I appreciate being in a Computer Engineering program. It is set up in such a way that the student can choose whether to place a heavier emphasis on the Electrical Engineering aspects or the Computer Science/Software Engineering aspects (which is what I'm doing). Furthermore, from what I've been seeing lately, computer engineers have a lot of diversity in choosing a position upon graduating. I've seen some do robotics and hardware engineering, and others (like myself) work in finance and do software development or even study further to do quantitative analysis (and make a ton of money in doing so). From the way I see it, it really is a major better suited for the vocational student, and the average salaries and hiring rates in this profession stand as a testament to that.

KingTermite
09-16-08, 09:19 AM
Here's the paradox about computer science:

It's advertised as a major that will assist you in learning how to develop software, but it really is the science of computing (which is MUCH more profound than the modern computer) and, to make matters worse, some schools place very little emphasis on actual software development! To make matters worse, employers see these students as potential assets for development instead of what (many) areare really trained for, which is a continuation in the study or an entry-level research position. Fortunately, at my school, a heavy dose of programming is included in their curriculum, along with the essentials that build a computer science graduate. It's no surprise, then, that there are many graduates who graduate with little working knowledge of how to code. In fact, if you take a visit to Slashdot and read up some Ask Slashdot threads, that is actually a common complaint amongst more seasoned developers and managers.

That is precisely the reason why I appreciate being in a Computer Engineering program. It is set up in such a way that the student can choose whether to place a heavier emphasis on the Electrical Engineering aspects or the Computer Science/Software Engineering aspects (which is what I'm doing). Furthermore, from what I've been seeing lately, computer engineers have a lot of diversity in choosing a position upon graduating. I've seen some do robotics and hardware engineering, and others (like myself) work in finance and do software development or even study further to do quantitative analysis (and make a ton of money in doing so). From the way I see it, it really is a major better suited for the vocational student, and the average salaries and hiring rates in this profession stand as a testament to that.

My school had a decent amount of programming too. This girl in question had already by this time had (at minimum) two programming classes (Ada, C) and a software engineering class under her belt. There was no excuse.


I understand what you mean about the CE major which is why *I* chose that direction too. However, my school (at the time - 10 years ago) did not have a strong software engineering emphasis (just one undergrad class - which I didn't take). I focused on more the hardware (EE) side of things, but had a new experience after graduation. For the time and the area I lived in, Computer Engineering was too new and many employers didn't know what it was. In the end, I ended up starting out as a software test engineer and worked into software engineering. Though I focused more on hardware at the time, I'm pretty much fully a software engineer now (though much of my software is to manipulate hardware).

MrCrassic
09-16-08, 09:21 AM
now do it without using the mod operator..



for (int i = 0; i<=100; i++) {
if (i/(i/3)==3) //this division should equal the multiple.
cout << "Fizz";
else if (i/(i/5)==5) //this division should equal the multiple.
cout << "Buzz";
else if (i/(i/3)==3 && i/(i/5)==5)
cout << "FizzBuzz";
else
cout << "Number: "<< i;
**


Now to prove that this code works, let m be a multiple and i be the iterator. In the first case, you get m since when i = m , i/(i/m) = m/(m/m) = m/1 = m. For every successive case, when i = km , where k is a constant, i/(i/m)=km/(km/m)=km/k=m. QED (I think??)

KingTermite
09-16-08, 09:24 AM
Now to prove that this code worksAnd you can give us the runtime order in big O notation.

MrCrassic
09-16-08, 09:29 AM
for (int i = 0; i<=100; i++) {
if (i/(i/3)==3) //this division should equal the multiple.
cout << "Fizz";
else if (i/(i/5)==5) //this division should equal the multiple.
cout << "Buzz";
else if (i/(i/3)==3 && i/(i/5)==5)
cout << "FizzBuzz";
else
cout << "Number: "<< i;
**


Now to prove that this code works, let m be a multiple and i be the iterator. In the first case, you get m since when i = m , i/(i/m) = m/(m/m) = m/1 = m. For every successive case, when i = km , where k is a constant, i/(i/m)=km/(km/m)=km/k=m. QED (I think??)

Okay, stop looking for answers :)

This will run in constant time, since it's only one for loop and each if statement take O=1 to complete.

I seriously need to start coding again. I miss it!

huytheskigod
09-16-08, 09:38 AM
In the midst of a big PHP project...don't want to see anymore coding than need be at the moment...why for you post these things...Runs away quickly!

p.s.

#include<stdio.h>

main()
{
printf("Hello Foo World");


**

MrCrassic
09-16-08, 09:48 AM
You forgot the debugging code :p

trsidn
09-16-08, 09:53 AM
I'll just go to my trusty compiler and run these.
Oh wait. I don't have one:(

annc
09-16-08, 09:56 AM
If you're using one series of if-else blocks then the multiple of 3 and 5 condition must be checked first.

I guess the title is right. Sigh...

huytheskigod
09-16-08, 09:59 AM
I'll just go to my trusty compiler and run these.
Oh wait. I don't have one:(

http://gcc.gnu.org/

KingTermite
09-16-08, 09:59 AM
I'll just go to my trusty compiler and run these.
Oh wait. I don't have one:(

You don't know what you're missing!!!


Here are some links to help remedy that situation
http://sourceforge.net/projects/gcw/
http://www.microsoft.com/express/product/default.aspx
http://www.thefreecountry.com/compilers/cpp.shtml

trsidn
09-16-08, 10:05 AM
You don't know what you're missing!!!


Here are some links to help remedy that situation
http://sourceforge.net/projects/gcw/
http://www.microsoft.com/express/product/default.aspx
http://www.thefreecountry.com/compilers/cpp.shtml

actually I do:)

This is one reason I am a Civil Engineer. I had a couple CS courses when I was in the EE curriculum. Gave me fits.

KingTermite
09-16-08, 10:10 AM
actually I do:)

This is one reason I am a Civil Engineer. I had a couple CS courses when I was in the EE curriculum. Gave me fits.

Multiple Personality Disorder fits, Inigo?

trsidn
09-16-08, 10:10 AM
too many to count:innocent:

AllenG
09-16-08, 10:17 AM
Graecum est; non legitur

crtreedude
09-16-08, 10:17 AM
Okay, stop looking for answers :)

This will run in constant time, since it's only one for loop and each if statement take O=1 to complete.

I seriously need to start coding again. I miss it!

Your loop will run 101 times, just saying.

MrCrassic
09-16-08, 10:36 AM
It starts at zero. I'm definitely part of the 99% now.

mattm
09-16-08, 12:53 PM
for (int i = 0; i<=100; i++) {
if (i/(i/3)==3) //this division should equal the multiple.
cout << "Fizz";
else if (i/(i/5)==5) //this division should equal the multiple.
cout << "Buzz";
else if (i/(i/3)==3 && i/(i/5)==5)
cout << "FizzBuzz";
else
cout << "Number: "<< i;
**




now optimize this for speed...

btw i think your last else/if would never execute.

MrCrassic
09-16-08, 12:55 PM
now optimize this for speed...

btw i think your last else/if would never execute.

That's when you compile that using special GCC options!

Why so?

Bob Ross
09-16-08, 01:03 PM
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.


I swear to god I just recently came across a website of Drinking Games, and the above was listed as one. Not the write-a-computer-program part, just having people count aloud, but for multiples of three say “Fizz” instead of the number and for the multiples of five say “Buzz”. For numbers which are multiples of both three and five say “FizzBuzz”. Anyone who misses a Fizz, Buzz, or FizzBuzz opportunity has to drink a shot. I'm not kidding, I really did just come across this exact scenario.

KingTermite
09-16-08, 01:09 PM
Why so?
Because those are if/else if structures.

If it hits one, then it will automatically skip the others without even checking, hence the "else" part.

The last one would need just a regular IF and not be part of the if/else structure for it to execute.

mattm
09-16-08, 01:10 PM
Why so?

if either of the first two conditionals are true, the 3rd one (both are true) will be skipped, and you would never print "fizzbuzz"

so the code could work like this i think:



int m = 0; //if 'i' is a multiple or not
for (int i = 1; i<=100; i++) {
if (i/(i/3)==3) //this division should equal the multiple.
{
cout << "Fizz";
m = 1;
**
if (i/(i/5)==5) //this division should equal the multiple.
{
cout << "Buzz";
m = 1;
**
if( !m) {
cout << i;
**
m = 0;
**

i use 'm' to see if the current number is indeed a multiple of 5 or 3, so we don't have to run the divisions more than once.. or would a compiler optimize that redundancy out?

btw if this is wrong, it's ok; i'm just an SDE/T, not a true dev!

trsidn
09-16-08, 01:12 PM
my head hurts

MrCrassic
09-16-08, 01:34 PM
I think this will work too (I don't have a compiler right now):



for (int i =1 ; i<=100; i++) {
if (i/(i/3)==3 && i/(i/5)==5) //First, check if both conditions are met.
cout << "FizzBuzz";
else if (i/(i/3)==3) //Then check for even divisibility by 3.
cout << "Fizz";
else if (i/(i/5)==5) //Then check for even divisibility by 5.
cout << "Buzz";
else //If all tests fail, print the number.
cout << "Number: "<< i;
**

It already runs in constant time, so any speed gains will be negligible. Actually, using the mod operation is faster because it uses a different algorithm for finding the remainder. See this link (http://en.wikipedia.org/wiki/Modulo_operation) for more information.

BONUS QUESTION: Solve this problem using x86 assembly language.

KingTermite
09-16-08, 01:41 PM
BONUS QUESTION: Solve this problem using x86 assembly language.Wuss!

Use IBM 360 (with JCL run script) assembly.