Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My code was not efficient

 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just got a rejection call from a prominent company. It was the first time in my career that I failed to get a job that I really want (among ones that actually gave me an interview for) It was not a huge loss as I am currently employed. She said that it was "very close" and ask me if I would like to be contacted in 18 months. I said yes, of course.

For me, that "very close" meant that one of the interviewer really didn't like me. Among 5 rounds of interviews, I believe I did ok in 4 of them, but I really messed up royally in one round. I never gave it a second thought until I get the rejection, but when I really think of it... I really sucked then.

What really shattered my confidence is that when I asked her for feedback. She responded "my code were not efficient enough". Oh no... anything but that...

I remember completing one of interviewer's question early and we had a little chat, I even mentioned that I felt that I am a little better than average in terms of optimizing performance of a piece of code. I even gave a quick example of what I did in my previous job that fixes a huge performance issue and he was quite happy about it. I also added something like "When I optimize, I never sacrifice clarity for performance. For example I prefer objects over functions." It was not very well put but what I meant was that my thinking is very object oriented and flexible. But I think he gets what I meant and agreed with me and even complimented me on something I did earlier in his question and how he likes the way I encapsulate a piece of logic in a class.

Ok, here's the deal. When I code, I always count the number of operations in my mind and how expensive are those operations. I always ask myself "are they minimal?" I have always thought I have gotten quite good at it already.

I knew I sucked at my first round. I knew I sucked because I panicked after I realized I have gotten the requirement wrong and I have already written a whole whiteboard of code. I would have been able to fix it... If I wasn't in such an panic and running around like an ant on a heated frying pan. I don't exactly remember what I did but I just remember it was wrong solution after wrong solution because I have never calmed down to think about the problem.

On that round, my code has efficiency problem, for sure, but I really wanted to believe only because I am not myself. It's just hard when I felt I am being judged on something that I believe I am strong in.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Hsu wrote:Just got a rejection call from a prominent company.

I usually don't like long message. rejection at one point just one point! all is well ...
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized something
At one point I use the following code



instead of



I used the first one because I wanted to show off my knowledge in java collection utilities. In retrospect, I wonder if he thought the first one created a collection of size n... That would have really looked slow.

The general rule of thumb is that the bigger n is the faster the first one is compare to the second one. I should have defended all lines of my code as I am writing them!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fair, the first line does create a collection of size n. It just does it very efficiently.
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:To be fair, the first line does create a collection of size n. It just does it very efficiently.

Technically you are right, but I found this to be a misleading statement
It does create a "collection" which's c.size()==n but it does not allocate n piece of memories and that's what makes it efficient
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Hsu wrote:
Mike Simmons wrote:To be fair, the first line does create a collection of size n. It just does it very efficiently.

Technically you are right, but I found this to be a misleading statement
It does create a "collection" which's c.size()==n but it does not allocate n piece of memories and that's what makes it efficient


See: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#nCopies%28int,%20T%29
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Peter Hsu wrote:Just got a rejection call from a prominent company.

I usually don't like long message. rejection at one point just one point! all is well ...


Rejection is not what bums me out. It's being criticized for writing slow code... Really shattered my ego
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's misleading about it? And why belabor the obvious? Of course there's no reason to allocate n pieces of memory for identical copies. That's why I said it was efficient.
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:What's misleading about it? And why belabor the obvious? Of course there's no reason to allocate n pieces of memory for identical copies. That's why I said it was efficient.

I meant no offense. I agree with you then. I just thought that some people might take it as a big object got allocated.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think "very close" means they liked someone else was better. You could have gotten everything right and had still had that problem.

When I ask people to code, there isn't going to be one line of code that can kill the interview for them. It's easy to make up for one mistake.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Hsu wrote:Rejection is not what bums me out. It's being criticized for writing slow code... Really shattered my ego


All of the code I write is slow the first time.

As Donald Knuth wrote:
Premature optimization is the root of all evil.


Any company that is not smart enough to understand this, and understand why Knuth is right, is too stupid to want to work for. Your ego should be rejecting them.

Get the code to work. If its fast enough, go drink a beer. If not, measure what is slow and fix that.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said:

I never sacrifice clarity for performance.


If I was still a hiring manager I would LOVE that answer!
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Hsu wrote:I just realized something
At one point I use the following code



Unless either:
  • You are writing for an embedded system

  • or
  • "n" is much larger than a million

  • or
  • "n" is many thousand and you are doing this millions of times


  • There is no practical impact. In most cases, the JIT optimizer will make the actual operation identical. Not similar so no one cares, but actually and literally identical. The JVM's optimizer is amazing. Its probably the best of "the good parts" in Java. We've been using the JVM for nearly two decades, and folks have been working on optimizing it, and optimizing our code running on it since day 1.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic