Data Structures & AlgorithmsModerateGATE CS · DSA
Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [29, 18, 10, 32, 39, 19, 36, 13] and k = 4, what does it return, and how many elements does the first Lomuto partition (pivot = last element) place to the LEFT of the pivot?
- A. 19; 6
- B. 19; 1
- C. 29; 1
- D. 18; 1
Correct answer: B. 19; 1
The k-th smallest of [10, 13, 18, 19, 29, 32, 36, 39] is 19. The first partition around 13 puts the 1 smaller elements to its left; quickselect then recurses into only one side.
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
- A peak is an element larger than its neighbours (array ends count as −∞). Binary search runs on [43, 46, 49, 14, 48, 18, 24, 47, 28]…
- A fixed window of length 3 slides over s = "bcaacbccbbbbac" with a character-count array. At how many starting positions is the window an…
- For each element of [16, 4, 2, 19, 10, 6, 29, 9], find the next greater element to its RIGHT (−1 if none), using a monotonic stack. What…
- How many distinct non-empty substrings does the string "aaaaa" have, and how many substrings counted by position?
- Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed? python from collections import…
- A string of length n is searched for a pattern of length m. What are the worst-case times of the naive algorithm and of KMP?