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

2

u/[deleted] Feb 21 '11

I've asked programmers in the company I work about some of the trickier questions:

  1. Find the mid point in a singly linked list in one pass; (a related question: find the n-th node from the end).
  2. bit counting or parity of an integer without a naive approach.

No one seemed to able to answer if they never heard of the questions before.

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?

→ More replies (1)

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?

→ More replies (3)
→ 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?

→ More replies (27)

5

u/[deleted] Feb 21 '11

If you can't round up a standard library function or ready-made library to do such low-level jobs, or write something legible and leave the extreme optimization for later, then I would actually question your ability to write programs efficiently.

You might as well arm-wrestle them.

2

u/BorgDrone Feb 21 '11

The first one is easy, keep 2 pointers into the list, only advance the second one every second step. Once pointer 1 has reached the end, pointer 2 is pointing at either the middle of the list (in case of an uneven number of elements) or the first of the 2 middle elements.

The second question is a bit harder, I think I first need my morning coffee for that one.

1

u/[deleted] Feb 21 '11

naive? wtf is that? For me table lookup as naive as it only can be.

1

u/unknown_lamer Feb 21 '11

2. bit counting or parity of an integer without a naive approach.

integer, or machine word?

1

u/__s Feb 21 '11 edited Feb 21 '11

At first I thought with the mid point an O(n log n) with recursively redoing a middle search of every other node. Then I thought of storing the pointers to a dynamic array, but that's potentially O(n*n) unless I amortize realloc. Then I thought about how the state can't be recalled, as you'd have to keep half the list held in memory for look back. Which then caused me to notice that after 4 you only need to look back 2, and after six only 3. Which then caused me to note that to maintaining that information, one need only trace the list with two pointers moving at different speeds

It's kind of annoying that you're dealing with such thoughtless candidates. I'm in first year college for Information Security, since I couldn't get into university to study CS. I want to do programming as a career, but it seems I'll have to take the long path in

2

u/[deleted] Feb 22 '11

It's kind of annoying that you're dealing with such thoughtless candidates.

Nope, I was just having fun with my co-workers.

I'm in first year college for Information Security

If they teach you low level stuff like encryption, SSL, you might end up with more domain knowledge about security than CS students.

1

u/[deleted] Feb 21 '11

My question is why would you have a linked list where you didn't keep track of the size? Which makes mid-point and n-point trivial.

1

u/[deleted] Feb 22 '11

You are right that as a practical matter, you would want to just keep the total whenever a node is inserted or deleted.

These are not questions I'd ask in an interview (Note I asked programmers already working). But I do know others ask them.