Only k mod n matters, because n rotations put the array back where it started, which is why the code reduces k first. When that remainder is 0 the call becomes reverse(a, 0, -1), so reverse has to treat an empty or backwards range as a no-op instead of indexing past the end. An empty array has to be rejected earlier still, because k % a.size divides by zero before any reversal runs. In Kotlin, Java, C and Go the % operator keeps the sign of the left operand, so a negative k yields a negative remainder: rotate left by k either needs ((k % n) + n) % n or should be turned into a right rotation by n - k.