Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the key or at the start) sorts [33, 26, 53, 37, 25, 8, 24, 19]. How many element shifts and how many key comparisons does it make?
- A. 22 shifts, 22 comparisons
- B. 22 shifts, 29 comparisons
- C. 22 shifts, 26 comparisons
- D. 23 shifts, 26 comparisons
Correct answer: C. 22 shifts, 26 comparisons
Shifts equal the number of inversions (22). Each insertion makes one extra comparison that stops the loop unless the key travels to index 0, so comparisons = 26.
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
- What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1] together into…
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Items K, L, M, N, O are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- In a singly linked list with only a head pointer, which operation CANNOT be done in O(1) time?
- Run-length encoding: each maximal run is written as the character followed by its length. What is printed? python s = "yyyyyyyyxx" out, i…