Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: Sum the remainders of a list of signed deltas modulo 3 in C++ and in Python.

Problem: Sum the remainders of a list of signed deltas modulo 3 in C++ and in Python.
int a = -7 % 3;
int b = 7 % -3;
int c = ((-7 % 3) + 3) % 3;
cout << a << " " << b << " " << c;
What is printed?
  1. A. 2 -2 2
  2. B. -1 1 2
  3. C. -1 -1 2
  4. D. 2 1 2

Correct answer: B. -1 1 2

In C, C++ and Java, % keeps the sign of the dividend: −7 % 3 = −1 and 7 % −3 = 1 (Python would give 2 and −2). Adding the modulus and taking % again normalises to 0..m−1: 2. This matters for prefix-sum-mod tricks and negative array indices.

More Coding Interview Problems questions

Exams that ask this topic