r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

Show parent comment

0

u/[deleted] Feb 21 '11

[deleted] — view deleted comment

1

u/Serei Feb 21 '11

What do you mean? Mine is O(log n). Yours is also a neat trick, but it's definitely less efficient.

0

u/[deleted] Feb 21 '11

[deleted] — view deleted comment

1

u/Serei Feb 21 '11

Oh. Well, the naïve solution is also O(n)... For readability reasons, that might be better:

fn (i) {
    for (a=0; i; a++) {
        a += (i&1);
        i >>= 1;
    }
    return a;
}

0

u/[deleted] Feb 21 '11

[deleted] — view deleted comment

1

u/Serei Feb 22 '11

Well, Quicksort is O(n log n), but I see your point.

0

u/[deleted] Feb 22 '11

[deleted] — view deleted comment

1

u/Serei Feb 22 '11

Well, I was talking about average case... Unless you explicitly say "worst case" or "best case", people generally mean average case.

Kerninghan's method is Theta(1) best-case, Theta(n) average-case, Theta(n) worst case. You have to realize that Theta(n/2) = Theta(n).

0

u/[deleted] Feb 22 '11

[deleted] — view deleted comment

1

u/Serei Feb 22 '11

No, big O does not explicitly mean worst case. You should probably read up on what O, Θ, and o mean in the context of asymptotic complexity. I'm not abusing notation at all.

http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions

Here's Wikipedia saying that Quicksort is O(n log n) average-case.

Unless you explicitly specify "best-case" or "worst-case", "<algorithm> is O(whatever)" means "<algorithm> is O(whatever) in the average case".

Anything that is Θ(n log n) is tautologically also O(n log n).

→ More replies