As a graduate I went for an interview for a java development role and was doing pretty well in the technical examinations until i came up against this question.
If i
Algorithm F on (in-text) page 7 is exactly what you're looking for. :)
http://www-cs-staff.stanford.edu/~uno/fasc3a.ps.gz