Hello again.

I’ve been doing a little project for myself. I’m attempting to write a fairly simple program in multiple programming languages and compare them in many ways. I compared the writing in terms of difficulty of writing, amount of overhead, and the time the programs took to run. The program I decided to write was a simple program that would loop and calculate the value of Pi using the Gregory-Leibniz Series. This series (I found it here, thanks wikipedia). I used this way to calculate Pi because it was fairly simple to implement.

I have currently implemented the program in four languages: Ruby, Perl, C, and Java. The line counts for each of the programs is followed (with comments removed) directly from the “wc” command in UNIX. The exact line and output is as follows:

Ophanim:~/picalc/tempPi Gatacoma$ ls
picalc.c picalc.java picalc.pl picalc.rb
Ophanim:~/picalc/tempPi Gatacoma$ wc *
22 80 549 picalc.c
28 97 751 picalc.java
13 52 346 picalc.pl
13 41 248 picalc.rb
76 270 1894 total
Ophanim:~/picalc/tempPi Gatacoma$

The counts go Lines, Words, and Characters (I believe). As I said before, this was with removing the comments I made and also with removed any empty line in the code. Another difference is that the C and Java programs are coded to take a value at the command line, while the Ruby and Perl programs used a hard-coded number.

The variable-parameter is the number of iterations that the program goes though. A higher number is supposed to give a larger and more specific value of Pi. A limitation I’ve found, however, is that I cannot get a very high precision on my numbers. Anyone with an idea on how to get a larger precision on the numbers, I encourage you to speak with me about it.

I’ve chosen to run each of these programs with 100 million iterations. Which is a bunch. And now, the testing.

Ruby

Ruby is something. I’ve heard many good things about the language, and figured this was a good time to try it out. Ruby is an object-oriented scripting language that has implications far beyond what I’m doing with it. If I have the chance, I’d like to learn more about the language and how to use it.

It should also be noted that Ruby is an interpreted language, and I believe it’s speed suffers due to that. Interpreted means (for those who don’t know) that the language does not have a compiler and that you run the source code directly.

I found the ruby code to be rather easy to learn and implement, with little code that was there as just “Syntactic Sugar”

The source code for this script (commented, spaced) is below:

Edit: I just realized that the wordpress editor kinda ate my tabbing. My bad.


#
# picalc.rb
#
# Ruby program used to calculate pi using the
# Gregory-Leibniz Series as defined at
# http://en.wikipedia.org/wiki/Pi#Calculating_.CF.80
#
# @author Mark Stump
#

numerator = 4.0
denominator = 1.0
numiterations = 100000000
numAt = 1.0
negval = 1.0
answer = 0.0

numiterations.times do
answer += (negval) * (numerator/denominator)
negval *= -1
denominator += 2
end

puts "The calculated value of pi is"
puts answer

And now, the test:

The calculated value of pi is
3.14159264358933

real 2m22.319s
user 2m16.693s
sys 0m0.429s
Ophanim:~/picalc Gatacoma$

Perl

Ah yes, Perl. The “Swiss Army Chainsaw” of scripting languages. I had no experience with Perl before this experience (nor with Ruby), but I found it fairly simple to implement the program. However, I did notice something while writing the code: It seems like in advanced implementations, it could get really ugly, really fast.

Even so, I found Perl, like Ruby, very no-nonsense in it’s implementation and was very efficient in it’s coding.

Perl, like Ruby, is an interpreted language, so it is not compiled before it is run.

The source code:


# Script created for calculating Pi
# Uses the "Gregory-Leibniz" Series
# Found on http://en.wikipedia.org/wiki/Pi#Calculating_.CF.80
#
# Author: Mark Stump

$numerator = 4;
$denominator = 1;
$negVal = 1;
$calculatedVal = 0;

# Number of iterations for loop to calculate pi
$numIterations = 100000000;

for $1 (1 .. $numIterations) {
$calculatedVal += $negVal * ($numerator/$denominator);
$negVal *= -1;
$denominator +=2;
}

print "The calculated value of pi with ",$numIterations;
print " cycles of the calculation loop is: \n";
print $calculatedVal, "\n";

And the results:

Ophanim:~/picalc Gatacoma$ time perl picalc.pl
The calculated value of pi with 100000000 cycles of the calculation loop is:
3.14159264358933

real 1m0.654s
user 0m56.448s
sys 0m0.199s
Ophanim:~/picalc Gatacoma$

C

I like C. I have more experience with C++, but I haven’t felt like coding this program in that language yet. C is a language that is used for lower-level programming as well as operating-systems programming. From my experience, it is very fast and is generally efficient at what it does. I don’t really have enough experience with C to make many more observations, but I’m very sure I’ll become very familiar with this language as my time at RIT passes.

C is a compiled language, which aids in its efficiency and quickness.

Source code:

Edit: I can’t seem to get the source code to copy right. If anyone wants to look at it, feel free to contact me. Same as the java code.

The results:
Ophanim:~/picalc Gatacoma$ time ./picalcC 100000000
The calculated value of pi after 100000000 iterations is: 3.141593

real 0m1.607s
user 0m1.561s
sys 0m0.009s
Ophanim:~/picalc Gatacoma$

Beats the pants off of the interpreted langauges

Java
Ah Java, my old friend and nemesis. I have more experience in Java than any language, and I’m fairly comfortable with doing a good deal of implementations in it. This does not, however, mean that I’m particularly fond of it. Java is, like any other programming language, a tool. And any tool is good for its own set of jobs. You wouldn’t use a hammer to screw in a.. er… screw. I’ve heard (I haven’t tried) that Java is very good with graphic-type programs and that it also has a very good network interface. I do not have extensive experience with either, although I do like the simplicity that the Java.net package provides.

Java appears to be to have the most overhead of the languages I used. It was the only code where I had to catch an exception due to my converting of a string to an integer from the command line (I did the same thing in C, but C didn’t force me to catch an exception). Also, a good deal of my code in the program comes from my experience, and it may be unnecessarily bloated. Even so, I believe that Java requires more code than is necessary for what I am doing in this program.

Java, like C, is a compiled program. It differs from C in that it runs in a virtual machine that allows the Java program to be platform independent.

The results:

Ophanim:~/picalc Gatacoma$ time java picalc 100000000
The calculated value of pi after 100000000 iterations is:
3.141592663589326

real 0m1.717s
user 0m1.632s
sys 0m0.042s
Ophanim:~/picalc Gatacoma$

Closing Remarks

This was purely an exercise I did in my spare time. I enjoyed the time I put into this program, and felt that it gave me a wide spread of results which all proved to be interesting.

I tried to keep the code as similar in each program as possible. I did run into some snags (such as that fact that Ruby doesn’t have a “for” loop), but I felt I generally succeeded in keeping the programs very similar.

Finally, I realize that using scripting languages for something like this may be something akin to “Square Peg, Round Hole.” I didn’t do this to find the most efficient implementation of the algorithm; I did it to see how each language was to code under the same condition.

I plan to keep doing exercises like this, if I have the time. If anyone has any suggestions for an algorithm to try or something to implement, fell free to contact me at mas1447#at#rit#dot#edu.

Advertisements

Not such a winning combination

I had my third and final phone interview with Amazon.com last week sometime, and I am just now getting around to posting about it. This was the interview I tanked, and it is now why I hold a deep hatred for technical interviews over the phone. The phone interview consisted of one question.

One long question.

And I completely tanked it.

The question was “Given a string of digits, write a method that would return all possible combinations of strings that could be created using the phone number-digit thing”. I may have abridged the question a little; I forget the exact phrasing of the question. This wouldn’t have been such a huge deal.

Except I had to code over the phone.

This is something I’ve never experienced before, and is why I no longer like over-the-phone technical interviews. I probably wouldn’t like an in-person technical interview, but that’s besides the point. All programmers have their own little style to how they code. I, personally, like laying out a basic skeleton for the program or method I am going to write, and begin coding, adding variables and adjusting my algorithms as needed. This has always treated me well; and I find it a fairly acceptable way to program.

This style of coding, however, does not lend itself well to coding over the phone. I was told to talk the interviewer through my algorithm, which I did, and that is what brought to light the problems in my coding style. I kept finding things I should change or add, and I’m pretty sure I drove the interviewer near-insane. It didn’t really help that I was unable to find a solution to the problem. I was getting to a solution, but I ran out of time.

I’m not blaming the interviewer or Amazon or the interview process. I know that the responsibility for passing that interview fell squarely on my shoulders, and pass or fail, I was taking whatever was given to me. While I’m not blaming anyone, I feel I’m in the right to express my displeasure in the system. If anyone has any tips for phone interviews and coding over the phone, feel free to talk with me. I would greatly appreciate the chance to improve my interviewing skills.

Job fair is on Wednesday anyway. Plenty of fish in the ocean.

Phone interviews are something. They’re essentially quizzes of the entire breadth of your knowledge of the position for which you’re applying. In this case, I’m applying for a Software Development co-op at Amazon.com. This was my second interview of.. I don’t know how many are possible. Either way, this interview went fairly well, and I’ll put some of the questions asked below.

For anyone applying to a co-op at Amazon (or to any CS type job), take a look at this blog article.
The above link takes you to a page of Steve Yegge’s blog (Special thanks go to Noah Richards for the recommendation of that blog. Go check out Noah’s blog at noahsmark.com ). Yegge was (and may still be) and employee of Amazon.com, and that page gives a good breadth of questions that you may be asked for an interview. One of the questions in my last interview was taken directly off that site (the contact/grep question). It’s a good read, and is really helpful for preparation.

Now, for my interview.

Started off with a question about projects I’ve done. Just an open-ended question. Be familiar with a project you’ve done. No big deal.

My second question was a logical thinking question. I’m not going to get into what it was (if you want to know, just say it in a comment and I’ll explain it), but if anything, make sure of the limitations they put on the question. I spent most of time of the question with a false assumption, which I was able to recover from, but still, ask a lot of questions. It can’t hurt.

I then had two simple coding-type questions, one with working with a string and the other with shuffling a deck of cards. Again, no real deal.

The final questions I had dealt with Object-Oriented programming concepts. I was actually able to pick one topic to explain (I chose polymorphism) and the other the other I was given to explain (Virtual and Pure Virtual methods, as well as Virtual destructors).

This interview, unlike the last one, did not have an involved coding question where I was to submit code. The code questions I was asked covered this. I don’t know which is preferable; I find it a little hard to code over the phone (I tend to start writing code and then realize I need a variable which I didn’t declare, which is easy to fix in an editor, but can be confusing over the phone).

It seems that the phone interviews have a time limit. This time limit seems to be, for Amazon, about 45 minutes long.

Now I get to see where it goes from here. They’ll eventually contact me regardless of what they decide. Which is nice; waiting for something that’s not going to come isn’t fun. Hopefully, I impressed the interviewer enough to get me to the next round, so I can make a new article, called “Amazon Interview, Round 3 of ???”

Maybe this is just me, but I wasn’t aware of the multiple-interview format. This is probably how any large company screens it’s prospects. I’ll just have to get used to it.

Attached to this post is the code I made for the question I mentioned below. Feel free to point out any changes you would make or any suggestions. Let me just say that again, I was under the gun for this, and it’s also been a good deal of time since my last real Java problem solving experience.
Code

I’ve been wondering if I should do this for a while. Make and maintain a blog that I’m quite aware no one will ever read. Eventually, with one or two people saying it’s a good idea, I’ve decided to officially throw my hat into the blogging ring.

 

For those of you who don’t know me, my name is Mark Stump and I’m a second-year Computer Engineering major at the Rochester Institute of Technology. I’m also a lab instructor for the Computer Science Department at RIT, which is fun job to do.

 

The trigger for the creation of this blog was a job interview I just had. I’m currently trying to enter the BS/MS (Bachelors/Masters Dual Degree) program that the CE department offers, which means I have to co-op this summer. Today I had a phone interview with Amazon.com, and I wanted to get out how it went and what was asked. I’m sure many people I know will go through processes such as this; I just got lucky with an early interview. The interview consisted of two parts: Technical questions and a long coding question. What happened in each part is explained below.

 

Technical Questions

 

This part of the interview took about a half hour and consisted of a wide-range of questions, from basic Object-Oriented Programming concepts to advanced Java topics. I was able to get most of the questions, but one I completely borked when I shouldn’t have. That question is below:

 

Interviewer: “Explain the difference in Java between strings and string buffers”

 

Me: <Long winded, grasping-at-straws explanation>

 

Me: “That’s not even close, is it?”

 

Interviewer: “Nope”

 

Me: “Crap”

 

Which was not a good way to start (this was my first question). However, things turned around from there. Another question I was asked was why Java now allows you to use Generics in collection declarations. While I wasn’t completely sure about this, I was able to come to the correct answer (allows Strong Typing). While I didn’t say “Strong Typing” exactly, I was able to explain the concept of it, which seems like that’s what really counted

 

A simple question that was given that I felt was easy (and made me feel smart) was to explain what a garbage collector is and what it does.

 

Finally, the most involved question I received was to design a complete Zoo with the concepts of OOP. This wasn’t language specific, just general conceptual-type stuff. I gave an answer of a large “Zoo” class where everything else would come together, and then other classes such as an “Animal” class (which sub-classes that inherit for specific species), a “Cage” class (sub-classes for different types of cages), and a person class with subclasses for different people (Spectator, employee, whatever).

 

The best advice I can give is to not get discouraged. Like I said, although I completely borked the first question which I probably should’ve gotten, I was able to recover and finish fairly strong.

 

Coding question

 

This question could’ve gone down in a few ways. First, if I didn’t have enough time, I would’ve written out the code by hand and relayed the code over the phone, which had a really excellent chance of being an awful experience. Since I had the time, I was able to get the full version of the problem and probably show the interviewer that I have a decent grasp on the language I programmed the solution in and also that I have decent problem solving skills.

 

The question was this: Write a method (or methods) that take an integer and return the value as an English sentence. The number was to be between 999,999 and 0. This problem wasn’t overly hard, but it did take some time to complete.

 

To complete the problem, I was given about 2 hours, of which I took about an hour and a half to get a satisfactory solution (to me).

 

The method I attacked this problem was to use three methods, two of which returned string versions of basic numbers and the main driver method to convert the number and return a string. I’m not going to get into my solution for the problem (if you want, feel free to ask me for it, I’ll give it to you) since the logic behind, while I find it easy to understand, I suck at explaining stuff like this over the web.

 

Feel free to try it yourself; it’s actually a fairly fun, simple problem. I was able to do it a little under 200 lines of code (which comments I put in). My solution is probably bloated and given more time, I could probably trim the code to be a good deal more efficient and more compact. I was under the gun and a bit nervous, which isn’t an environment where I code the best. I was given the choice to do the code in any language any way I wanted. Java was the programming language I choose (I’m most familiar with it) and I also used the Eclipse IDE to test and code my program.

 

Anyway, that’s about all I have to say on this topic. I wanted to record my experience, and I hope the information I have helps at least one person with his or her own interview. If you have any questions or comments or whatever, feel free to contact me at mas1447@rit.edu.

 

Now I get to enjoy the rest of my Spring Break. Ciao.