Data Structures & AlgorithmsModerateTCS Digital · Infosys SP
For the sorted array [3, 3, 4, 5, 5, 7, 7, 8, 8, 8, 8, 10] (0-based), what are lower_bound(5) (first index with value ≥ 5) and upper_bound(5) (first index with value > 5), and hence how many times does 5 occur?
- A. 4 and 5; count 1
- B. 3 and 6; count 3
- C. 3 and 5; count 2
- D. 3 and 4; count 1
Correct answer: C. 3 and 5; count 2
lower_bound is the first 5 at index 3; upper_bound is one past the last 5, index 5. Their difference 2 is the count — both found by O(log n) binary searches.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many contiguous subarrays of [−2, 1, −3, −4, 8, 1, 4, −5, 6] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- What does this loop-counting code print? (Python) python n, c = 16, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- An equilibrium index i has (sum of elements left of i) = (sum of elements right of i), excluding a[i]. Which indices of [0, 9, 2, 3, 4, 2…
- A stack is implemented with two queues where push is costly (new element goes into the empty queue, then everything else is moved behind…
- Daily prices are [15, 17, 19, 13, 20, 17, 38, 38, 40]. What is the maximum profit with at most ONE buy and one later sell, and with…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "dbdcccdcbaddad" with all characters…