Wednesday, October 31, 2007

What happened to the $1. Can you explain? - A simple math brain teaser that leaves you thinking..

So you think you are good at Math and at simple calculations. Here we go. Try and solve this..

Three guys came to a motel and asked for a room with 3 beds. The manager said that the room rent was $30. They said they will check the room and pay for it. So the boy took them to the room, they liked it and each paid with a $10 note and the net amount came to $30. The boy went back to the manager and gave him $30. Then the manager realized that they had discounts at that time and that the room rent was actually $25 for the day. Since he was honest, he gave $5 (5 1 dollar notes) to the boy and asked him to return that money to those guys in the room. While going to the room, the boy thought that they cannot share the amount of $5 equally and since they didn't pay him the tip last time, he thought that he will take $2 and give them back $3 so that each gets $1 back. He silently slipped $2 into his pocket and went to their room and gave each guy $1. Everyone was happy.

But here is the question about it.. Initially the three visitor guys gave $30. Each paid $10 and got $1 back. This means each paid $9 and so the total they paid is $27. $2 were stolen by the hotel boy. We have the count for $27 + $2 = $29. What happened to the remaining $1 ???

How much do you understand your auto loan - Can you find the fault in this calculation?

A small discussion with my room mate brought up the subject of auto loan that I have on my car and why I should / should not pay it off. I have some savings in my savings account and I have an auto loan on my car. If I open a CD (Certificate of Deposit) with Indymac bank they will give me an APR of 5.5% and my car loan is financed by a Credit Union at 5.5%. The argument that my roomie was making was that I should pay off the car loan instead of opening a CD. His reasoning was that, though the car loan and the APR on the CD were the same, I am actually losing money, because on the interest that I earn on the CD, I have to pay a tax. And since I am single and fall in the higher tax bracket, I will end up paying 25%(+) on the interest earned. Which means that I will actually be making around 75% of the 5.5% which is around 4.13% which means that I am losing around 1.37% (5.5-4.13) on the whole amount that I am planning to open a CD with. In a way, it made sense for a brief moment.

But then, further investigation showed that it is not a valid argument to me. Here was my case that I presented. Let us say you have $10000 with you, and you have two choices, put it in a CD for two years at the rate of 5.5%, or pay off your loan which is 5.5% and ends in 2 years. If you don't pay off the loan now, you keep paying it in monthly amounts of $440.96 (Calculation from Bankrate calculator) with your salary. If you put it in CD for 2 years, you make a simple interest of $1130.25 (550+550). Let us say, you pay 28% tax on it. So your net interest earned from that amount after paying taxes off is 72% of 1130.25 which is $813.28 and the total interest you pay on the car loan for these 2 years is $582.96 (Calculation from Bankrate calculator). So I argued that I actually am making $230.32 (813.28-582.96) extra by not paying off the car loan.

Sounds logical right, but the above calculation is wrong because it was one-sided. The reason is, if I pay off the car loan with the $10000, then I don't need to pay them the $441 that I was paying every month, which means that I can put it in a savings account like ING Direct for an APY of 4.3% or an APR of 4.21%. This means that the total amount I make in two years with this monthly savings would be $11541 (Calculation from dinkytown). This means I make an interest of $1541 in two years. After cutting the tax at the rate of 28%, I will be left with around 72% of 1541 which is $1109.52. This means the total I save is this 1109.52 plus the interest I would have paid for the car loan which is 582.96. So the net total I would have saved is $1692.48 So by not paying off the loan today, I am actually losing is $879.20. So the conclusion of the whole calculations is it is better to pay off the car loan, if you have the cash instead of saving in your bank account and having the loan aside and paying monthly payments.

Agreed this calculation has some flaws because of some assumptions. It assumes that the rate from ING Direct will be 4.21%, but since it is variable it can go up or come down. Also, we are assuming that you will have continuous money from your salary, and that you will actually put aside the car loan amount payment in a Savings account instead of spending it on the next cool thing like the Iphone :) It also assumes that you would pay the money off instead of having it in your savings account, which you can withdraw in case of an emergency. You obviously can't get a car loan again on your car in case of an emergency right :) It also assumes you have 10k or the total amount of your car loan with you in your savings. But anyways, under the given set of assumptions, it makes sense to actually pay off the car loan rather than put the amount in the CD or Savings account. One more thing, you can consider is taking the amount from a special offer like 0% APR (be careful with the Balance Transfer fee in the fine print) on Balance Transfer from a credit card company like Citi and paying off the loan, provided you have a good credit history. That way, you don't pay the interest to the auto loan provider and you can pay off monthly amounts to the credit card company. And when the balance transfer offer ends after one year, take another card and transfer the balance to that. All this provided you actually have time to do some research on these and wish to save some dollars :)

Feel free to challenge the above calculations or present counter arguments :)

PS: There is a big fundamental mistake in the above calculation, and the net amount you actually save is $272.19 and not $879.20 as mentioned above. Can you prove this?

Tuesday, October 23, 2007

What do you work for? Money, Fame or a Happy Relaxed retirement??

A lunch time discussion yesterday got me thinking about what do I actually work for.. At lunch, my team lead told us about his friend's Mom who expired at 60. And he was like, I would be pretty disappointed with myself if I die at 60. His reasoning was simple. One works till about 60 years thinking that he/she will retire at 60, and then with all the money saved, he can just relax, buy a great car, and relax and do nothing on that private yacht just sitting down on a chair in the yacht on a sunny day relaxing. But let's say, you work hard and toil till 60 thinking of that day, that you would have enough money to just sit and relax, and guess what, you just die at 60. Then what's all this hard work worth?

You may say, I am doing this for my children. But the point is, you will be tuning the minds of your children, the same way your parents did to yours, as to how important it is to be in top 10 percent in the class, as to how it is important to be educated well enough to lead happy and secure life. And you know what, they will toil just like you do, and the vicious cycle always keeps continuing. No, I am not telling you Rich Dad-Poor Dad story to forget saving to your 401 (k) and paying your bills like the Poor dad and start investing like Rich Dad. Either ways, you are toiling towards something, which you are never gonna enjoy actually. You earn when you should be enjoying, and by the time you have enough money, you do not have age to enjoy your life. The long term benefits are indeed way too long to achieve.

So, does that mean that you should stop studying, or working and then start enjoying your life doing nothing. That is not what I am hinting at. You need to understand and plan, when you need to relax, may be take a year off in thirties and start fresh again, and again take a year off in your forties and relax probably. Yes, you might not get those lucrative pay hikes, and those well deserved promotions in your organization, but come to think of it, it is probably well worth it. If you can't give yourself 1 year out of ten years of your life, then who and what are you living for.. If at all, you live to see your golder seventies and glorious eighties, you will still not regret your taking off those years because you dared to enjoy you life when you had the zeal and the age to enjoy.. Think about it, it might be something that you could put in your achievements to cherish at a later age. May be this might not be the best solution, but it is one of the feasible solutions to balance your craving for a relaxed life and your hectic work schedules..

Wednesday, October 10, 2007

statement.setMaxrows in JDBC vs Oracle rownum in SQL

If you want to limit the number of rows retrieved from an Oracle database in a Java/Servlet application using JDBC, you can accomplish this in two ways. One is using statement.setMaxRows in the JDBC or using rownum in the Oracle SQL.

For example, lets say there are 1000 employee records in the EMPLOYEES table which has the columns: empid, first_name and last_name. In order to retreive the first 200 rows, in java you can say


Connection dbConn = db.getConnection();
Statement stmt = dbConn.createStatement();
String sql = "select empid, first_name, last_name from employees";
ResultSet rs = stmt.executeQuery(sql);


This gets you a resultset with 200 rows and everybody is happy. You could have alternatively done that using rownum on your sql query itself without using setMaxRows() on statement like below:


Connection dbConn = db.getConnection();
Statement stmt = dbConn.createStatement();
String sql = "select empid, first_name, last_name from employees where rownum<201";
ResultSet rs = stmt.executeQuery(sql);

Even this time, the resultset has 200 rows and everybody is happy. Now the question is, which one should you use and why. I may not be an expert in talking about this, but here are my observations...

If you are dealing with a table which has relatively less records like in thousands, then either way works just fine. But if you are dealing with potentially huge database which has like millions of records, then I guess using rownum in the sql makes more sense. This is because the impact on your Java heap space would be lesser.

According to JDBC, the implementation of setMaxRows is as given below:
The maxRows limit is set to limit the number of rows that any ResultSet can contain. If the limit is exceeded, the excess rows are silently dropped.

For example, lets say there are 14 million records in a table, from which you want to retrieve the first 3000 rows. If you use statement.setMaxRows(), the JDBC call retreives the entire 14 million records from the database table and sets the first 3000 records on the resultset object. But the retrieval of such huge number of records puts a huge lump of data on your JVM, and you are dependent on the Garbage Collector for retrieval of this space. But if you use rownum in the SQL, then the number of rows retrieved from the DB by JDBC itself is limited and there won't be any heap space issues. I ran a trial session with our DBA and it was confirmed that the statement.setMaxRows() doesn't modify the sql using rownum automatically. We traced the session and it was retrieving all the million records. And I came to a conclusion that the heap space issues I was facing was due to the usage of statement.setMaxRows() instead of using rownum on the SQL. Also the speed of retrieval of rows when you limit by rownum could be more, though I don't have substantial evidence to state it as a fact.

If you are an expert in Java / Oracle Databases and feel that part/total of this post doesn't make any sense or if it looks like complete bullshit, then please feel to share your expertise on this matter / bash me in the Comments:)

Tuesday, October 2, 2007

Happy Days - A Nostalgic Retreat

Watched Happy Days (Telugu) movie in Irving Hollywood theaters this week. What a way to remember the happiest days of my life, viz college days. As the emotions of students joining the engineering college overflow mellifluously on the screen, one cannot help but bring these characters to real life experiences, with expressions like Shankar in the movie was like this guy in the college and so on. 4 years of Engineering - from fear of ragging to ragging of juniors in 2nd year, from a novice of life to a career planner of life, from few friends before engineering to numerous friends, from nobody to an 'Engineer' - thats the life you have experienced that unfolds on the celluloid leaving you with a tinge of smile on the lips and a nostalgic feeling in heart..

The story is simple. 4 guys and 2 girls come to CBIT as engineering students. The life that they encounter with love, friendship, career choices and emotions is the movie. The entire credit goes to an adept handling by Sekhar Kammula, who gives us back our precious college memories. I don't remember the last time that I had taken a moment to remember all my Engineering classmates and about my experiences with them. But after seeing the movie, I went back to my nostalgic memories. Yes, I went through ragging and yes I did rag a few juniors in college within limits, and I was one of the many students who used to scold our college and I did love the samosas in the fifth gate of our college with a mini Pepsi :) But in the last two years, I probably never spent a moment in remembering them to bring a smile on my lips, but seeing the movie, all I could think of the next whole day was my engineering college days.

The music by Mickey J Meyers is simply superb and the lyrics of the song Oh My Friend are just awesome. I would have listened to this song like 100 times so far. And the characters are simply lovable. The best is Tyson with a caring heart and sincere love for a senior. The next is Rajesh who cares a lot about his friends, my favorite among the girls is Tamanna and also Sonia who played the senior. And the theater in which the movie was showed was really good. Kudos to the exhibitors who maintained great standards. Once in a while, a movie comes that reminds you of memories that you cherish, and lets you associate yourself with the characters and feel their characters instead of seeing the actors in those characters. Happy Days is one such brilliant movie. A really good watch worth every penny spent :)