Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is applied to [27, 5, 47, 17, 3, 34, 46, 41]. What is the array after 3 passes, and how many swaps will the complete sort make?
- A. [5, 3, 17, 27, 34, 41, 46, 47]; 5 swaps in total
- B. [5, 17, 27, 47, 3, 34, 46, 41]; 5 swaps in total
- C. [3, 5, 17, 47, 27, 34, 46, 41]; 5 swaps in total
- D. [3, 5, 17, 47, 27, 34, 46, 41]; 7 swaps in total
Correct answer: C. [3, 5, 17, 47, 27, 34, 46, 41]; 5 swaps in total
After 3 passes the 3 smallest values sit in front: [3, 5, 17, 47, 27, 34, 46, 41]. Over the whole sort, 5 passes find their minimum elsewhere and swap, so 5 swaps (at most n − 1).
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
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…
- For an n × n matrix whose rows and columns are both sorted ascending, what is the time of the staircase search for a key?
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Partition a linked list around x: all nodes with value < x come first, then the rest, each part keeping its original relative order. What…
- Two ways of finding the middle of a linked list with slow/fast pointers. What is printed? python h = build([25, 47, 35, 52, 83, 28, 17…