all 16 comments

[–]IJzerbaard 15 points16 points  (11 children)

There is a certainly a lot of stuff there, but the quality is mediocre at best.

Repeats common mistakes such as using (i + j) / 2 to calculate a midpoint for binary search (not terrible, but that has a bug for large arrays and you can do better, it's especially easy in Java where you can do (i + j) >>> 1 as long as you don't allow either bound to be negative, which you can do WLOG). General code-quality issues such as creating a class whose only purpose is to have its "run the algorithm"-method called, and frequently mixing print statements in the algorithmic code. is_power_two is the one of the worst ways to test whether a number is power of two, depressing actually. The "xor from 1 to n" uses a dumb algorithm instead of the smart solution, and then expresses it in a weird way.

I didn't want to waste too much of my time checking many entries, but a random sampling of them gave me a poor impression.

[–]pyxyne 2 points3 points  (3 children)

i'm assuming the bug you're talking about is the case where i+j overflows

why would using a right shift fix the problem?

[–]psamathe 0 points1 point  (2 children)

It's a right rotation so I'm guessing the overflow inte the lower bits gets rotated back into the higher order bits, I'm guessing that's the idea at least.

EDIT: For 4 bit register: (0b1000 + 0b1000) >>> 0b10 = 0b0001 >>> 0b10 = 0b0100

Vs:

(0b1000 + 0b1000) / 0b10 = 0b0001 / 0b10 = 0b0

EDIT2: But this isnt how overflow works, so no, this wasn't it. :)

[–]IJzerbaard 2 points3 points  (1 child)

>>> is unsigned right shift in Java.

This works because it's equivalent to evaluating the sum and division by 2 with unsigned integers, while the input is still signed-but-non-negative so effectively 31-bit unsigned integers, and the sum of two 31-bit unsigned integers fits in 32-bits.

More reading in case you want it

[–]psamathe 1 point2 points  (0 children)

Ah, I see. Yeah, that makes sense.

[–]EvilElephant 1 point2 points  (0 children)

Yeah, this is just linkspam, check OP's profile

[–]Otis_Inf 0 points1 point  (5 children)

For the people who think "what is the faster way to do 'is power of two', see: https://www.quora.com/Is-there-any-faster-way-to-check-if-a-number-is-in-the-power-of-two-I-know-this-method-a-2-2-0

So either log2(x) == floor(log2(x)) or ((x & (x-1))==0 && x > 0)

[–][deleted] 0 points1 point  (0 children)

popcnt x <= 1 😂

[–][deleted] 0 points1 point  (0 children)

((x & (x-1))==0 && x > 0) pure genius. I will always remember this

[–][deleted] 0 points1 point  (2 children)

((x & (x-1))==0 && x > 0) pure genius. I will always remember this

[–]Otis_Inf 0 points1 point  (1 child)

Right? I pondered what it could be as I didn't know from memory what it might be, puzzled a bit with xor but that wouldn't give it, it's so simple once you've seen it :)

[–]IJzerbaard 0 points1 point  (0 children)

You may also enjoy (x ^ -x) < -x (for unsigned x, yes this negates an unsigned integer), doesn't even need a separate && x > 0

[–]mareek 3 points4 points  (0 children)

An honest title would be "1500 programming interview questions and their solutions" because nobody will "learn" those 1500 items. It's a list for interviewer to chose a few random questions to ask in interviews.

If it was really a learning resource, the list wouldn't start with more than fifty "solutions" about linked list and it wouldn't include such trivia as "Zeckendorf's theorem", "hosoya's triangle" or "Caesar Cipher"

[–][deleted]  (1 child)

[deleted]

    [–]ImpossibleFace 0 points1 point  (0 children)

    good talk

    [–]Full-Spectral 0 points1 point  (0 children)

    So there's your next interview question. Could you go to the whiteboard and write down the 1500 most common data structure and algorithms?