Data Structures & AlgorithmsHardTCS NQT Digital · language semantics
Problem: What does the following print?
Problem: What does the following print?
print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))
- A. -3 2 -3 -3
- B. -5 1 -4 -3
- C. -4 1 -4 -3
- D. -3 1 -3 -3
Correct answer: C. -4 1 -4 -3
Python // floors toward negative infinity, so -7 // 2 = -4 and -7 % 2 = 1, while int() truncates toward zero, giving -3. Languages such as C truncate, which is the trap.
More Coding Interview Problems questions
- Problem: What does the following print? python print(bin(12 & -12), 12 & -12)
- Problem: Coins of values {1, 3, 4} are available, unlimited copies each. To make amount 6, the greedy algorithm always takes the largest…
- Problem: What does the following print? python def ed(a, b): m, n = len(a), len(b) dp = [[0] (n + 1) for in range(m + 1)] for i in range(m…
- Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?
- Problem: What does the following print? python a = list(range(10)) print(a[::-3], a[2:8:4])
- Problem: Count subarrays whose sum is divisible by k=5 for a = [4, 5, 0, -2, -3, 1]. python from collections import Counter def f(a, k): c…