Avoid Using Strings in Loops and Thoughts on Interview Street
|Just a picture of code.|
First, I would like to mention the site Interview Street. It's aim is to be a resource for companies looking for talented developers. As a job seeker, your task is to solve programming questions. Your code needs to pass a couple of test cases to get the full credit. Some of the important factors is having your code get all the correct answers and speed. If it's still running after a certain time period then you will get a "CPU time limit exceeded" message and thus you fail on the current test case. If you answer (fully) at least 6 questions, you can apply to the companies listed there which includes some big names like Apple, Facebook and even companies like Dropbox.
My reactions to the site is mixed. After checking the site again, there are some UI (User Interface) changes. There is a need for more changes in my opinion. Below are some of my observations.
- The new look is more streamlined allowing you to navigate faster
- The old UI featured search parameters on the leaderboard but it seems to be gone in the redesign
- The icon for status for each challenge question doesn't mesh with the overall theme of the site
- The information regarding your submission is now more detailed. You can now see the running time of your code (Excellent and much needed)
- The information on some companies is lacking way too much. I wondered for a second if the server was down. No picture or info about certain companies (one even having gibberish data in the company details)
- Gone is the ability to search company by location (critical). I noticed there are now 3 Canadian companies and a lot of new Indian companies along with a lot of companies from CaliforniaI don't know if the site is for a new graduate like me though. I can't see/don't know the details of some of the people who answered. It would be interesting to know whether I'm competing with experienced developers (and know how much experience they have). I'll probably try out some more problems or tweak the ones I answered (to pass all the test cases) when I have a little more time.
Back to the original topic. Like I mentioned, I was working on some String problems. I noticed in the article that there's a huge difference when using String vs StringBuilder. I already heard about some performance benefit of using StringBuilder but I didn't know it was that drastic. I might try it out in my code and see how much it makes a difference (I'll update the article).
In a nutshell, the reason String is slow is because it is immutable (can't be changed after creation). What this leads to is that when your are concatenating or using the "+" operator in combining Strings, you are creating Strings for the old one and the new one (with the combined data). If you are using a big String then there is more information to be copied and thrown away. If you're manipulating a large String then the performance time increases dramatically.
It's much better explained in the article I mentioned. You can also read the debate in the comments. Here is a similar article but it's an interview with a "Java Champion". Both articles are interesting reads but remember that both are a bit old so there could be some improvements in Java 7 (I think it wasn't mentioned in the changes I've read but correct me if I'm wrong).
An interesting test as the interview with the Java Champion Mr. Kabutz mentioned is too test your code. Measure the running time and compare the difference. A good habit to have.
Thanks for reading!