Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The sorted array [1, 4, 4, 9, 12, 12, 15, 20] (0-indexed) may contain repeated values. Using a lower-bound search, which returns the first index whose value is at least 12, what index is returned?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
The lower bound is the first index holding a value at least 12, here the first occurrence of 12. Using bisect_right returns the index after the last 12 instead.
More Coding Interview Problems questions
- Problem: How many subarrays of [1, 2, 2, 3] have max minus min at most 1? Run the code. python a = [1, 2, 2, 3] print(sum(1 for i in…
- Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the…
- Problem: What does this run-length encoder print for 'aaabccdd'? python s = 'aaabccdd' out = [] i = 0 while i < len(s): j = i while j <…
- Problem: Check bracket balance for '([]{})' and '([)]' with a stack. What is printed? python def ok(s): pairs = {')': '(', ']': '[', '}'…
- Problem: Sum the values of the next greater element of each position in [2, 1, 4, 3], where a position with no greater element contributes…
- Problem: Using at most one flip of a 0 to 1, what is the longest run of consecutive 1s in [1, 0, 1, 1, 0, 1]? Run the sliding-window code…