Data Structures & AlgorithmsHardTCS NQT Digital · Python semantics
Problem: What does the following print?
Problem: What does the following print?
print(2 ** -1, 7 % -3)
- A. 0.25 2
- B. 0.5 -2
- C. 0 -2
- D. 0.5 -1
Correct answer: B. 0.5 -2
Negative exponents give floats, so 2 ** -1 = 0.5. Python's modulo takes the sign of the divisor, so 7 % -3 = -2.
More Coding Interview Problems questions
- Problem: What does the following print? python print(sorted([3, 1, 2], key=lambda x: -x)[0]) What is printed?
- Problem: Starting from 3, you may apply two operations: "+1" or "×2". What is the minimum number of operations needed to reach exactly 50?
- Problem: A knight starts at square (0,0) on an infinite chessboard. What is the minimum number of knight moves needed to reach square (4,5)?
- Problem: The complete graph K6 has 15 edges. A perfect matching of 3 disjoint edges is removed. How many edges remain?
- Problem: What is the chromatic number of the cycle graph C5 (5 vertices in a single cycle)? The chromatic number is the least number of…
- Problem: What is the independence number of the cycle graph C7 (the maximum number of vertices with no two adjacent in the 7-cycle)?