Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
For the sorted array [1, 3, 4, 6, 6, 9, 9, 11, 13, 14, 14, 15] (0-based), what are lower_bound(14) (first index with value ≥ 14) and upper_bound(14) (first index with value > 14), and hence how many times does 14 occur?
- A. 9 and 12; count 3
- B. 9 and 10; count 1
- C. 9 and 11; count 2
- D. 10 and 11; count 1
Correct answer: C. 9 and 11; count 2
lower_bound is the first 14 at index 9; upper_bound is one past the last 14, index 11. 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
- Records are sorted with a composite key. What is printed? python recs = [('kabir', 80), ('dev', 70), ('meera', 70), ('ravi', 70), ('zoya'…
- Stock prices over 8 days are [21, 87, 49, 49, 28, 34, 81, 65]. The span of day i is the number of consecutive days ending at day i…
- You must check whether a singly linked list of n nodes is a palindrome using O(1) extra space. Which approach works in O(n) time?
- The intervals [4, 8], [13, 15], [5, 8], [8, 11], [12, 16], [8, 11] are sorted by start and merged (intervals that touch, like [2, 4] and…
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [17…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [2…