Data Structures & AlgorithmsVery hardProduct company OA
An array is split into the MAXIMUM number of contiguous chunks such that sorting each chunk separately and concatenating them gives the fully sorted array. How many chunks are possible for [3, 1, 2, 6, 4, 5, 7, 9, 8]?
- A. 3
- B. 5
- C. 9
- D. 4
Correct answer: D. 4
For a permutation of 1..n, a chunk can end at position i exactly when max(prefix) = i. Prefix maxima: 3, 3, 3, 6, 6, 6, 7, 9, 9 → equal to i at i = 3, 6, 7, 9 → chunks [3 1 2] [6 4 5] [7] [9 8]. Forgetting that [7] stands alone gives 3.
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
- An extended Boyer–Moore vote keeps two candidates to find every value occurring MORE than n/3 times, then verifies them with a second…
- Only the head pointer of a singly linked list of n nodes is known (no length is stored). What is the time to reach its middle node?
- An empty deque performs pushfront(1), pushfront(2), pushfront(3) and then popback(). Which value is removed?
- A sorted array holds n elements and has spare capacity. What is the worst-case time to insert a new element so that the array stays sorted?
- Counting sort on [2, 0, 2, 1, 0, 2] (values 0–2) builds count[v] and then turns it into cumulative counts, count[v] = number of elements ≤…
- Two pointers start at both ends of the sorted array [1, 3, 4, 6, 8, 11] to find pairs of DIFFERENT positions with sum 12 (move left if the…