my implementation of interview code, java version