Data Structures & AlgorithmsModerateTCS NQT · Accenture pattern
Bubble sort (each pass compares adjacent pairs left to right and swaps if out of order) runs on [6, 2, 9, 1, 5]. What is the array after the first TWO passes?
- A. [2, 6, 1, 5, 9]
- B. [1, 2, 5, 6, 9]
- C. [2, 1, 6, 5, 9]
- D. [2, 1, 5, 6, 9]
Correct answer: D. [2, 1, 5, 6, 9]
Pass 1 carries 9 to the end: [2, 6, 1, 5, 9]. Pass 2 carries 6 to the second-last slot: 2 < 6 (no swap), 6 > 1 swap, 6 > 5 swap → [2, 1, 5, 6, 9]. [2, 6, 1, 5, 9] is the state after only one pass; the fully sorted array needs a third pass.
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
- The array [1, 2, 3, 4, 5, 6] is first rotated RIGHT by 2 positions and then rotated LEFT by 5 positions. What is the result?
- What is the longest common prefix of the strings interview, internet, interval and intern?
- The first k = 4 elements of the queue 1, 2, 3, 4, 5, 6 (front first) are reversed using a stack: dequeue 4 items onto the stack, pop them…
- k sorted lists contain N elements in total. They are merged into one sorted list by repeatedly taking the smallest head from a min-heap…
- A deque of indices gives the MINIMUM of every window of size 3 in [4, 2, 12, 3, 8, 1, 7]. What are the window minima?
- A hash table of size 10 uses linear probing. Slots 2, 3, 4, 7 and 8 are occupied and all others are empty (no deletions have happened). An…