Your circular index works until it steps back from zero

Anything that wraps around, a playlist, a ring buffer, a rota, ends up with (i + k) % n somewhere. You test moving forward, it wraps past the end and comes back to 0, and you move on. Then the interviewer asks what happens when the user presses "back" on the first track, and the answer in Java is an index of -1.

The situation

A playlist holds n tracks in an array. The player keeps the current position and has one method that moves by k steps: positive for "next", negative for "previous". The first version is one line:

int skip(int current, int k, int n) {
    return (current + k) % n;
}

With five tracks, skip(3, 4, 5) gives 2, which is right: 3, 4, 0, 1, 2. Every forward test passes, including the ones that wrap.

The input that exposes it

The smallest breaking input is one step back from the start: skip(0, -1, 5). You want 4, the last track. Java returns -1, and the next array read throws.

The hidden assumption is that % always gives a value from 0 to n - 1. In Java it does not. The remainder takes the sign of the left operand, because integer division rounds towards zero and the two are defined together: a == (a / b) * b + a % b. So -1 / 5 is 0, and -1 % 5 is -1.

Python behaves differently. Its // rounds down, and % takes the sign of the right operand, so (0 - 1) % 5 is 4 and the one-line version is correct there. That is exactly why this bites people who practise in Python and interview in Java, or who switch between the two in the same week.

The common patch makes it worse, because it looks finished:

return (current + k + n) % n;   // skip(0, -1, 5) == 4

This works for a step back of up to n. A user who jumps back seven tracks calls skip(0, -7, 5): that is -2 % 5, which is -2 again. The patch moved the edge; it did not remove it.

The same rule breaks a second, quieter case: putting coordinates into grid cells. In Java x / 10 gives cell 0 for both -1 and 1, so cell 0 is twice as wide as every other cell, and nothing throws. Python's -1 // 10 is -1, as you would expect.

The invariant and the fix

Say the rule the index must keep: the position is always in the range 0 to n - 1, whatever the sign or size of the step. Then use the operation that keeps it:

int skip(int current, int k, int n) {
    return Math.floorMod(current + k, n);
}

Math.floorMod(-1, 5) is 4 and Math.floorMod(-7, 5) is 3, the same answers Python gives. For grid cells, Math.floorDiv(-1, 10) is -1. If k can be very large, current + k can overflow before the remainder is taken; reduce the step first with (current + Math.floorMod(k, n)) % n.

In Python the one-liner is fine, but one trap remains: int(a / b) and math.fmod round towards zero, like Java, so int(-1 / 10) is 0. Use // and % together.

What to say out loud

Before you write the line, say the range and the sign: "The index has to stay between 0 and n minus 1. The step can be negative, so I need a remainder that never goes below zero. In Java that is floorMod, not %."

When you test, say the edge you are choosing and why: "Forward wrapping is easy. I will try one step back from 0, and then a step back larger than n, because that is where the usual plus-n patch fails."

If you are in Python, say it anyway in one sentence: "Python's modulo follows the divisor's sign, so this is safe here; in Java I would use floorMod." It shows you know where the line would break, not only that it happens to work.

The exercise

  1. Without running anything, write down what -1 % 5, -7 % 5 and -1 / 10 give in Java, and what (0 - 1) % 5 and -1 // 10 give in Python.
  2. Check your answers in a Java shell and a Python shell, and mark every one you got wrong.
  3. Write skip with the plus-n patch, then find by hand the first value of k that breaks it for current = 0, n = 5.
  4. Rewrite it with floorMod and trace k = -1, k = -7 and k = 12.
  5. Search your recent code for % n or / size on a value that could be negative, and write the range each one is meant to keep.

Where this fits in Gadfly

Gadfly has four rooms; in the algorithms room you code in Java or Python, and its catalog of 711 problems is tagged by the pattern each one trains. In tutor mode it asks the next question and never reveals the solution, so "what does your index do one step back from zero?" is the kind of question it asks, and "I'm stuck" gives one more step, up to pseudocode, never finished code. In mock mode it behaves like an interviewer, with no coaching during the round and a debrief at the end quoted from the session record.

Published 30 November 2026 · One mistake, one counterexample · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].