Data Structures & AlgorithmsModerateAmazon · Microsoft OA
Problem: Use bisect to count how many readings lie in the closed range [2, 7].
Problem: Use bisect to count how many readings lie in the closed range [2, 7].
from bisect import bisect_left, bisect_right a = [1, 2, 2, 2, 5, 7, 7, 9] L, R = bisect_left(a, 2), bisect_right(a, 7) print(L, R, R - L)What is printed?
- A. 1 6 5
- B. 3 7 4
- C. 1 7 7
- D. 1 7 6
Correct answer: D. 1 7 6
bisect_left(a, 2) = 1 (the first 2) and bisect_right(a, 7) = 7 (just past the last 7), so the range [2, 7] holds 7 − 1 = 6 values: 2, 2, 2, 5, 7, 7. Using bisect_left for the right end would drop the 7s.
More Coding Interview Problems questions
- Problem: Vertical order traversal: group nodes by column (left child c − 1, right child c + 1); inside a column sort by row, then by…
- Problem: Zigzag level-order traversal (left to right, then right to left, alternating). python class T: def init(s, v): s.v, s.l, s.r = v…
- Find the bug: this BST validator compares each node only with its own children. python class T: def init(s, v): s.v, s.l, s.r = v, None…
- A binary tree with 12 nodes is serialised in preorder, writing '#' for every missing child. How many '#' markers appear?
- Problem: Return the diameter of a binary tree (number of EDGES on the longest path between any two nodes). python class T: def init(s, v)…
- Problem: A node of a directed graph is 'safe' if every path from it ends at a terminal node (no outgoing edges). List the safe nodes…