Data Structures & AlgorithmsVery hardProduct company OA
What is the smallest positive integer missing from [3, 4, −1, 1, 7, 2, 2, 9]? (In-place cyclic placement: put each value v with 1 ≤ v ≤ n at index v − 1, then scan.)
- A. 6
- B. 8
- C. 10
- D. 5
Correct answer: D. 5
Values in 1..8 are placed at their own index: 1, 2, 3, 4 and 7 find their slots; −1, the duplicate 2 and 9 (> n) are left wherever they land. The first index i with a[i] ≠ i + 1 is i = 4, so 5 is missing. The duplicate check a[v − 1] ≠ v prevents an infinite swap loop on the two 2s. '10' (n + 2) or '8' come from ignoring the gap at 5.
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
- Which problem CANNOT be solved by a plain variable-size sliding window that shrinks from the left?
- The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 5, 5, 7, 7, 7, 7, 7, 3]. What candidate and final…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 2 positions. What is the final array?
- Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…