JOIN
Get Time
forums   
Search | Watch Thread  |  My Post History  |  My Watches  |  User Settings
View: Flat (newest first)  | Threaded  | Tree
Previous Thread  |  Next Thread
<< PREV    [ 1 2 3 4 5 ]
Re: Foiled by a Div2-250! (response to post by tster123) | Reply
I wasn't saying your results are wrong. By the common sense using StringBuilder/Buffer should be defenietly faster than creating new String objects in each step. It was just a mandatory remark that one needs to be careful before drawing conclusions from VM language benchmarking.
Re: Foiled by a Div2-250! (response to post by slex) | Reply
I knew as soon as I saw those links that they were to posts by Pops :). Probably got something to do with the fact that in two of them he was replying to me ;).
Re: Foiled by a Div2-250! (response to post by aussie) | Reply
He can be taught! :)
Re: Foiled by a Div2-250! (response to post by aussie) | Reply
I thought when I read the benchmarks that I should reply with my best Pops voice, but I decided not to bother. Of course, like you, I recognized slex's best Pops impersonation and considered replying to it with something like "who died and made you Pops?" (also without actually clicking on the links).
Re: Foiled by a Div2-250! (response to post by Kawigi) | Reply
First here you admitted that you had stopped yourself from posting, and now you are saying you've done it again twice?
Are you on some 12 steps program for posters?
Re: Foiled by a Div2-250! (response to post by d000hg) | Reply
I'll steal my thread back to ask:
a)
If they hand me a pad of paper and say "I want a function to do...", and there is a spare PC handy, is it rude to ask to use that instead? I hate writing code on paper and it distracts me from the problem I'm trying to solve?

b)
If you are asked "write a function to..." would you assume they want error checking on pointers and so on, or just are looking for the algorithm? You can ask of course - "do you want to see me solve it or do you want to see me write production-level code" - but at my last interiew they were into letting me decide what I wanted to write!
Re: Foiled by a Div2-250! (response to post by d000hg) | Reply
If I was the interviewer...

a) Write it on paper. I know it's more difficult, you need to do it without keeping changing and recompiling the code and I would like to see if you can.

b) The working algorithm would be enough, if you want to add some validations it's fine too.
Re: Foiled by a Div2-250! (response to post by slex) | Reply
I'd say do at least basic validation (i.e. null checks). If you don't, they might just ask you to do it later, but might as well get it out of the way :-)

From my experience, being able to answer questions like "what is the runtime complexity" and "what is the space complexity" are also important :-)
Re: Foiled by a Div2-250! (response to post by d000hg) | Reply
I would implement exactly what I am asked first, without any validation. I would spell out the preconditions in comments. Then I would explain in what situations I would also write validation code and then I would also write the validation code. If they don't want the validation code they will stop you. What is important is to communicate what is your plan and what are you are doing while you are writing code. When you finish writing the code you should have a pretty good idea on what input it works.

In industry you would write validation code almost for each method. In the unlikely case that you have tools that check automatically that private methods are always called with valid data (which could be done for simple things such as nullness) then you can skip the ugly validation code.

Why do I say it's ugly? See these two pieces of code.

private Rational div(Rational a, Rational b) {
  if (a == null) throw BlaBlaException("error in Rational.div: null a");
  if (b == null) throw BlaBlaException("error in Rational.div: null b");
  if (b.getDenominator() == 0)
    throw BlaBlaException("error in Rational.div: division by zero");
  return new Rational(
    a.getNumerator() * b.getDenominator(),
    a.getDenominator() * b.getNumerator()
  );
}
And this.

/*@ requires b.getDenominator() != 0; */
private /*@non_null*/ Rational div(/*@non_null*/ Rational a, /*@non_null*/ Rational b) {
  return new Rational(
    a.getNumerator() * b.getDenominator(),
    a.getDenominator() * b.getNumerator()
  );
}
For the second one there are compilers that generate the equivalent of the IFs in the first one. There are also static tools that check whether the contract is always satisfied. It's shorter, though not as short as it could be if backwards compatibility would not be an issue. For example, in Spec# it looks like this.
private Rational! div(Rational! a, Rational! b) 
requires b.getDenominator() != 0;
{
  return new Rational(
    a.getNumerator() * b.getDenominator(),
    a.getDenominator() * b.getNumerator()
  );
}

The big problem is that the tools I'm talking about, especially the ones that do static verification, are far from being good enough for normal use. But we'll get there :)

edit: the method should be static to make sense and I didn't pay too much attention to correctness (i don't have a coeherent way of treating a 0 denominator). The point was only to illustrate what can be done if the language supports contracts. [oh, and the first time i mixed denominators/numerators -- I always forget which is which in English. I had to fix that].
Re: Foiled by a Div2-250! (response to post by Stroker_Ace) | Reply
void reverse(char *abc1) {
 
	char c;
	char *abc2=abc1;
      
        if(!*abc1) return;
        while(!*abc2) abc2++;
        abc2--;
        c = *abc2;
        *abc2 = 0;
        reverse(abc1+1);
        *abc2 = *abc1;
        *abc1 = c;  
}
 
<< PREV    [ 1 2 3 4 5 ]

RSS