Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: What does this Python snippet print? print(-7 // 2, -7 % 3, 7 // -2)
- A. -3 2 -3
- B. -4 -1 -3
- C. -4 2 -4
- D. -3 -1 -4
Correct answer: C. -4 2 -4
Python's floor division rounds toward negative infinity and the modulo takes the sign of the divisor. Truncating toward zero (as in C) gives different values for the negative operands.
More Coding Interview Problems questions
- Problem: What does this snippet print? gen = (xx for x in range(4)) print(sum(gen), sum(gen))
- Problem: What does this snippet print? fs=[lambda: i for i in range(3)] print([f() for f in fs])
- Problem: What does this snippet print? s = "abcdefgh" print(s[::-3], s[5:1:-2])
- Problem: What is the value printed by this snippet? from collections import Counter print(Counter("abracadabra").mostcommon(2))
- Problem: Consider the list a = [2, 4, 6, 8]. The loop 'for x in a: if x % 2 == 0: a.remove(x)' is run. What is the list a afterwards?
- Problem: How many recursive calls are made in total when naive Fibonacci fib(n) = fib(n−1) + fib(n−2) with fib(0) = fib(1) = base cases…