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?
- A. 2 -2 2
- B. -1 1 2
- C. -1 -1 2
- 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
- Problem: Sum the prices of n = 100000 items, each 50000, in Java. java int n = 100000, price = 50000; int total = n price; long total2 = n…
- Problem: Count primes below n with the sieve of Eratosthenes. python def countprimes(n): if n < 3: return 0 isp = [True] n; isp[0] =…
- Problem: Compute nCr mod p for many queries using precomputed factorials and Fermat inverses (p prime). python M = 109 + 7 N = 20 fact =…
- Problem: Find the GCD of all array elements and the LCM of a pair. cpp long long g(long long a, long long b) { return b == 0 ? a : g(b, a…
- Problem: Count trailing zeros of n! (number of factors 5). python def tz(n): c = 0 while n: n //= 5; c += n return c print(tz(25)…
- Problem: Reverse the digits of a 32-bit signed integer, returning 0 if the result overflows. python def rev(x): s = -1 if x < 0 else 1 r =…