Loading exercise...

Exercise: Balanced Position

Avatar image for athlete369

athlete369 on Nov. 15, 2026

balanced_position([0, 0]) outputs 0

This should return -1 ! Because there won’t be left or right.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Nov. 20, 2026

@athlete369 0 is the expected result here. It’s actually one of the examples in the exercise description.

The key is how an empty side counts. At index 0 there are no values on the left, and the sum of no values is 0:

>>> sum([])
0

So for [0, 0] at index 0, the left side is [] with a sum of 0, and the right side is [0] with a sum of 0. Both sides are equal, so index 0 counts as balanced. The same rule is why balanced_position([8]) returns 0, where both sides are empty.

This lines up with how prefix sums work in Keep Totals You Can Reuse, where the running total starts at zero because there are no values before position zero.

Only accepting positions that have values on both sides would be a valid variation of the problem. This exercise just follows the convention that an empty side sums to zero.

Become a Member to join the conversation.