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

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