Data Structures & AlgorithmsEasyTCS NQT Digital · Product Company SDE
Problem: A sorted list of ticket prices is searched with Python's bisect module.
Problem: A sorted list of ticket prices is searched with Python's bisect module.
from bisect import bisect_left, bisect_right a = [10, 20, 20, 20, 35, 50] print(bisect_left(a, 20), bisect_right(a, 20), bisect_left(a, 36), bisect_right(a, 5))What is printed?
- A. 1 4 4 1
- B. 1 3 5 0
- C. 2 4 4 0
- D. 1 4 5 0
Correct answer: D. 1 4 5 0
bisect_left returns the first index whose value is ≥ x (1 for 20), bisect_right the first index whose value is > x (4, just past the last 20). 36 would be inserted before 50, at index 5; 5 goes before everything, index 0. bisect_right − bisect_left = 3 is the count of 20s. Each call is O(log n).
More Coding Interview Problems questions
- Problem: Orders (name, priority) are sorted by priority only, using Python's sorted with a key. python orders = [("ravi", 3), ("asha", 1)…
- Problem: A juice stall sells a ₹5 cup; customers pay with ₹5, ₹10 or ₹20 notes and the stall starts with no change. Decide whether every…
- Problem: Builds 1..100 are tested; every build from some version onward is broken. isbad() is expensive, so find the first bad build with…
- Problem: Sort values by increasing frequency; values with the same frequency go in decreasing order. python from collections import…
- Problem: A researcher's h-index is the largest h such that at least h papers have ≥ h citations each. python def hindex(c): c = sorted(c…
- Problem: A 3 × 4 matrix is sorted row-wise and each row starts after the previous row ends. Search it as one virtual sorted array of R·C…