I had to write a while loop in an interview recently, and I also made the off by one error on my first attempt. But after I ran through a couple test cases by hand, it was pretty easy to see the mistake and fix it.
I wouldn't say getting it right on the first try is the most important thing. Writing code, logically thinking it through, and running through test cases to fix mistakes is pretty similar to how coding on the job is, albeit with less contrived problems.
I wouldn't say getting it right on the first try is the most important thing. Writing code, logically thinking it through, and running through test cases to fix mistakes is pretty similar to how coding on the job is, albeit with less contrived problems.