Data Structures & AlgorithmsHardAmazon · Microsoft OA
Edge case: chained comparisons and booleans as integers in Python.
Edge case: chained comparisons and booleans as integers in Python.
print(1 < 2 > 1.5, (1 < 2) > 1.5, True + True, 3 > 2 == 2)What is printed?
- A. True False 2 True
- B. True True 2 False
- C. False False 2 True
- D. True False True True
Correct answer: A. True False 2 True
1 < 2 > 1.5 means (1 < 2) and (2 > 1.5): True. (1 < 2) > 1.5 compares True (= 1) with 1.5: False. bool is a subclass of int, so True + True = 2. 3 > 2 == 2 means (3 > 2) and (2 == 2): True.
More Coding Interview Problems questions
- Edge case: closures over a loop variable declared with var versus let in JavaScript. javascript const a = [], b = []; for (var i = 0; i <…
- Problem: Minimum number of swaps (any two positions) needed to sort an array of distinct values, by counting cycles of the permutation…
- Problem: Count subarrays whose sum lies in [lo, hi] using prefix sums and a sorted list (bisect). python from bisect import bisectleft…
- Problem: Starting at index s, you may jump from i to i + a[i] or i − a[i]. Can you reach any index holding 0? python def canreach(a, s)…
- Problem: Count non-empty subsequences whose minimum + maximum ≤ target (sort, then two pointers with powers of 2). python def count(a, t)…
- Problem: Rotate an array right by k in O(1) extra space with three reversals. python def rotate(a, k): n = len(a); k %= n def rev(i, j)…