Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [5, 42, 29, 4, 41, 8, 53, 7]. What does the array look like after pass 2, and how many swaps have been made so far?
- A. [5, 4, 29, 8, 41, 7, 42, 53]; 8 swaps
- B. [5, 29, 4, 41, 8, 42, 7, 53]; 8 swaps
- C. [4, 5, 8, 29, 7, 41, 42, 53]; 8 swaps
- D. [4, 5, 29, 42, 41, 8, 53, 7]; 8 swaps
Correct answer: A. [5, 4, 29, 8, 41, 7, 42, 53]; 8 swaps
Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [5, 4, 29, 8, 41, 7, 42, 53], using 8 swaps in total (the swap count equals the inversions removed so far).
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
- Stock prices over 8 days are [27, 32, 42, 42, 35, 66, 59, 31]. The span of day i is the number of consecutive days ending at day i…
- Assertion (A): Binary search runs in O(log n) time on a sorted singly linked list. Reason (R): Binary search halves the search range in…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- A monotonic deque reports the maximum of every window of size 3 in [0, 13, 9, 1, 2, 18, −1, −3, 17]. What is the SUM of all reported…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0] together into…