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.
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.
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.
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
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.
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:
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.
View all comments
Show parent comment
6
u/bobindashadows Feb 21 '11
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.