Data Structures & AlgorithmsModerateProduct Company SDE · binary search warm-up
Problem: For the sorted array [1, 2, 2, 2, 5, 7], how many values are at most 2? Run the code.
Problem: For the sorted array [1, 2, 2, 2, 5, 7], how many values are at most 2? Run the code.
import bisect a = [1, 2, 2, 2, 5, 7] print(bisect.bisect_right(a, 2))What is printed?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: B. 4
bisect_right returns the index just after the last 2, which is 4, and equals the count of elements at most 2. bisect_left would give 1 instead.
More Coding Interview Problems questions
- Problem: How many strings of length 3 over the alphabet {a, b, c} have no two equal adjacent characters? Run the code. python from…
- Problem: How many divisors does 720 have? Run the code. python print(sum(1 for d in range(1, 721) if 720 % d == 0)) What is printed?
- Problem: How many ways are there to decode the digit string '111' (1 maps to A, ..., 26 maps to Z)? Run the DP. python s = '111' dp = [1]…
- Problem: What is the length of the longest substring without repeating characters in 'pwwkew'? Run the sliding-window code. python s =…
- Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code. python from itertools import…
- Problem: What is the sum of C(10, k) over all even k from 0 to 10? Run the code. python from math import comb print(sum(comb(10, k) for k…