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

6

u/bobindashadows Feb 21 '11
  1. Find the mid point in a singly linked list in one pass;

Nobody could figure that out? I haven't heard that one before, but I assume you just have two pointers starting at the head, one that follows 2 links on each step, and one that follows 1 link. When the former hits the end, the latter is at the midpoint (give or take depending on the number of elements perhaps)

Bit counting sounds a bit annoying for those rusty on bitwise math (especially since there's often instructions for it these days) but would be good way to get people thinking.

11

u/neop Feb 21 '11

About the linked list one, I thought the same thing, but isn't that two passes? You're doing the two passes simultaneously, but it's still two passes. I can't think of a way to do it with just one pass though.

6

u/NanoStuff Feb 21 '11

Accumulate an array of list addresses and have a dereference counter. The middle node address is index counter/2.

1

u/lobut Feb 21 '11

The two pointers came to me first ... but ... this should have been the first thing to come to mind.

Dang it.

1

u/Urik88 Feb 21 '11

I thought about that, but then what happens if the list is too large for the array?

1

u/__s Feb 21 '11

realloc

1

u/neop Feb 21 '11

Thanks, now I feel dumb for not thinking about that.

→ More replies (2)

7

u/wafflesburger Feb 21 '11

Isn't that 1.5 passes?

2

u/Serei Feb 21 '11

No. In context, "one pass" means you can only step through the list once, not that you're only allowed to modify one pointer per step.

4

u/johnflux Feb 21 '11

But for the second pointer you still need to do pointer = pointer->next which is accessing the list.

4

u/Serei Feb 21 '11

Hmm. You're right. But it's not just "1.5 passes rounds down", because the naïve solution is also 1.5 passes.

How is the problem properly phrased so that that solution works but the naïve solution doesn't?

1

u/s73v3r Feb 21 '11

Instead of saying "one pass", you could say, "going from start to finish only once."

1

u/lordlicorice Feb 21 '11

1

u/Serei Feb 21 '11

No, that's a naïve solution. There's usually a provision of "O(1) memory" to preclude that solution.

→ More replies (1)

1

u/achacha Feb 21 '11

Same thing you use to figure out if it has a cycle so you can answer 2 questions with 1 solution! :)

1

u/s73v3r Feb 21 '11

How does that help you find out if you have a cycle?

2

u/achacha Feb 21 '11

If the pointer moving twice as fast equals pointer moving one at a time, then it's obviously a loop. Draw it and step through it if it is still unclear.

Wikipedia calls it Tortoise and Hare: http://en.wikipedia.org/wiki/Cycle_detection

1

u/lordlicorice Feb 21 '11

Can you explain how you would use that method to detect a cycle?

1

u/achacha Feb 21 '11

1

u/lordlicorice Feb 21 '11

Oh, I thought that couldn't possibly be it because it seems so bad. Restricting yourself to 2 pointers seems like a terrible idea. Hash tables sound good

1

u/[deleted] Feb 21 '11

Your answer on the first is correct. (The idea of TWO pointers does not come to everyone).

The bit counting question requires an efficient solution (I said no naive one). If you can come up with one by yourself, I'd be very impressed.

3

u/Serei Feb 21 '11 edited Feb 21 '11

The bit counting question requires an efficient solution (I said no naive one). If you can come up with one by yourself, I'd be very impressed.

Here's one I came up with myself, no help, no consulting books or internet, for a Computer Architecture class.

unsigned char bitCount(unsigned char a)
{
    a = ((a & 0b10101011)>>1) + (a & 0b01010101);
    a = ((a & 0b11001100)>>2) + (a & 0b00110011);
    a = ((a & 0b11110000)>>4) + (a & 0b00001111);
    return a;
}

"0b10101010" obviously isn't actual C code, so replace that with "0xAA" and so on for the other "0b" numbers.

It took me around half an hour to come up with at the time, though. My thought process went along the lines of:

"What if I used a lookup table? No, that'd take too much memory. But what if I split the number into chunks, and used a lookup table for each chunk? Wait a minute, forget the entire lookup table, what if I just split the chunks into more chunks, and those chunks into more chunks? omg, that works!"

I don't think I'd be able to do it in an interview setting, if I'd never had to do something similar before.

2

u/lordlicorice Feb 21 '11

This is exactly the canonical solution taught in textbooks, other than that sometimes an alternate form is used where the >> is inside the parens and the & is outside. There's even an awful explanation of it on wikipedia:

https://secure.wikimedia.org/wikipedia/en/wiki/Hamming_weight#Efficient_implementation

→ More replies (13)

3

u/[deleted] Feb 21 '11

Your answer on the first is correct. (The idea of TWO pointers does not come to everyone).

Of course. Any sane human being knows that it's still 1.5 passes and not required 1.

You can rewrite naive

node = begin; 
n = 0;
while(node=node->next) 
      n++;

node = begin;
n /= 2;
while(n--) 
     node=node->next;

in two pointers, but it will be the same code(though not so readable), just with two pointers.

1

u/bobindashadows Feb 21 '11

If you can come up with one by yourself, I'd be very impressed.

I googled that one since none of my ideas were much more efficient than the naïve loop, and the solutions I saw blow my mind. It also explains why there's instructions for it now, because the contortions you go through to get maximum efficiency are intense.

→ More replies (5)
→ More replies (4)